How to instantiate an array in java – How to Instantiate an Array in Java?

How to instantiate an array in java: In this article we will see how to instantiate an array in Java programming language.

How to Instantiate an Array in Java?

Array instantiation java: As in the previous article How to Declare an Array in Java? We got to know that array is an object in java which can hold a collection of similar type data elements. Also we saw how to declare an array along with various examples. Now, we will after declaring an array how we can instantiate an array.

When we are saying instantiate an array that means we are allocating some memory space to that array. We already know in java to instantiate something we use the 'new' keyword. So, to instantiate the array also we will use the same new keyword.

Instantiation of an One Dimensional Array:

arrayName = new datatype[size];

Where,

  • arrayName: Refers to the name of the array (reference variable) which we have already declared.
  • new: Refers to the new keyword which is used to instantiate the array.
  • datatype: Refers to the type of array means array will contain elements of that type only.
  • [size]: Refers to the actual size of array. By using which memory is allocated accordingly.

Example:

Example-1 (Instantiating one dimensional integer array)

result = new int[10];

Here in the above example int is the type of array, result is the reference variable(array name) and [10] is the size of array.

Example-2 (Instantiating one dimensional String array)

city = new String[5];

Here in the above example String is the type of array, city is the reference variable(array name) and [5] is the size of array.

Combining Both Declaring and Instantiating in one Statement:

We saw how to declare and instantiate an array separately, like below.

Example:

int result[];                           //Declaring the array
result = new int[10];             //Instantiating the array

We can also declare and instantiate an array in a single statement like below.

Synatx:

dataType[] arrayName= new datatype[size];
Example:

int result[] = new int[10];  //Declaring and instantiating the  array

This was the example of one dimensional array like that multi dimensional array also can be declared and instantiated in a single statement.

Instantiation of an Multi Dimensional Array:

arrayName = new datatype[row size][column size];

Where,

  • arrayName: Refers to the name of the array (reference variable) which we have already declared.
  • new: Refers to the new keyword which is used to instantiate the array.
  • datatype: Refers to the type of array means array will contain elements of that type only.
  • [row size][column size]: Refers to the actual size of multi dimensional array(matrix). By using which memory is allocated accordingly. row size refers to the size of row and column size refers to the size of column.

Example:

Example-1 (Instantiating multi dimensional integer array)

result = new int[3][3];

Here in the above example int is the type of array, result is the reference variable(array name) and [3][3] is the size of array(here 2D array).

Example-2 (Instantiating multi dimensional String array)

city = new String[3][3];

Here in the above example String is the type of array, city is the reference variable(array name) and [53[3] is the size of multi dimensional array(here 2D array).

Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.

Related Java Articles: