How to declare and initialize an array in Java

We can declare and initialise an array in Java using a data type with square brackets, and a variable name, just like any other variable. You can assign a value to an array while it is being created by using curly brackets. Just like any other variable in Java, you can declare an array by providing it a type and a name. You can initialise or instantiate an array as you declare it in Java, allowing you to assign values as you go.

We can use the array by using the new keyword, followed by the array’s data type and the array’s size in square brackets. The size specified in the square brackets is used to create memory. The array’s items will all be of the same data type. When you declare and initialise an array, the values are stored in memory while the array is being declared. In this post, we’ll look at how to declare and initialise arrays in Java.

Array Declaration in Java

The declaration of a Java array is similar to that of any other variable declaration in Java. The array is specified with square brackets, either with datatype or variable. The array declaration does not allocate any memory and is initially filled with null or default values. There are two types of syntax that can be used to declare an array.

int a[];
int[] a;
String strArray[];
String[] strArray;

Array Initialization in Java

The declared array is initialised with the new keyword, followed by the datatype and size provided in square brackets. The new data type will allocate the memory size given in the square bracket. The array item values can be assigned once the array has been initialised with the new keyword and the array’s size. The size of the array cannot be changed after it has been initialised.

int[] a ;
a = new int[5];
a[0] = 10;
a[1] = 20;
a[2] = 30;
a[3] = 40;
a[4] = 50;
String[] strArray;
strArray = new String[2];
strArray[0] = "Yawin";
strArray[1] = "Tutor";

Array initialization with zero size

Java allows to initialise an array with zero size. The zero size array is not equals to array with null. If an array is declared without any initialisation, the array will be assigned with null. If an array is initialised with zero size, the array is not null and assigned with empty. The NullPointerException will not be thrown in this case.

int[] a ;
a = new int[0];

Array Declaration with Initialisation in Java

The array can be initialised when it is being declared. When the array is declared, it is initialised with values enclosed in curly brackets.

int[] a = {10, 20, 30};
String[] s = {"One", "Two", "Three"};

When the array is declared, the new keyword is also used when assigning values. The following syntax shows how to initialise an array with new keyword.

int[] a = new int[]{10, 20, 30};
String[] s = new String[]{"One", "Two", "Three"};

Array Initialization using Java Stream

In Java 8, objects can be utilised as streams. The stream allows for the initialization of values and the creation of an array. For declaring and initialising array values, the IntStream interface has three methods.

The following code is used to initialise an array with continuous integer values.

// starting from 0 till 9. (the value 10 is not included in the array)
int [] a = IntStream.range(0, 10).toArray(); 

//starting from 0 till 10. (it included the boundary values 0 and 10)
int [] a = IntStream.rangeClosed(0, 10).toArray();

The array can be created using the discrete values in the java streams. Use the command below to create an array of discrete values.

// discrete values in the same order
int [] a = IntStream.of(2,23,75,52,43,36,63).toArray(); 

//discrete values sorted in ascending order
int [] a = IntStream.of(2,23,75,52,43,36,63).sorted().toArray();

Multidimensional array declaration

The multidimensional array is declared using the techniques below. Multiple square brackets are used to declare a multidimensional array in Java.

int[][] a ;
int a[][];
int[] a[];

Multidimensional array initialization

The following syntax can be used to declare a multidimensional array.

int[][] a = new int[3][4];
int a[][] = new int[3][4];
int[] a[] = new int[3][4];

Array is initialised with values while declaring the array.

int[][] a={ {1,2,3,4}, {2,3}, {4,3,2} };

Array is initialised with values after declaring the array.

int[][] a = new int[3][4];

Multidimensional array with different size

The multidimensional array is created with different size. The following example will show how to create multidimensional array with different sizes.

int[][] a = new int[3][];
a[0] = new int[10];
a[1] = new int[4];
a[2] = new int[6];