Arrays in Java: Array Initializations (Part-2)

In my previous post, we have already learned how to declare an array, allocate the space for its elements, and how to modify and access its elements. Now we learn how to initialize a java array.

Although Java provides a default initialization for array elements, with arrays of objects, the default initialization results in object references set to null. This may result in runtime exceptions in the code if the elements are not initialized to proper object references.

There are two techniques for initializing the array elements:

·         The elements of the array may be initialized at runtime via value assignment (as shown earlier in first post)

·         Initialized via array literals

Initialization at Runtime

An array element may be initialized at runtime using its index value, as discussed in the previous post. For example, consider the following declaration for an array of integers:

int[ ] integerNumbers = new int[10];

Each element may now be initialized to a desired value by using a block of code such as the following:

integerNumbers [0] = 15;

integerNumbers [1] = 9;

integerNumbers [2] = 185;


integerNumbers [9] = 54;



If we decide to initialize all the elements of the array to the same value, we may use one of the loop constructs. For example, the following for loop will initialize all the elements of the preceding array to zero:

for (int i = 0;  i < 10;  i++) {

integerNumbers [i] = 0;



Initializing using Array literal:

Array literals provide a shorter and more readable syntax while initializing an array. For example, consider the following program statement:

int[] IntegerNumbers = {25 ,12, 39, 2, 198};

This statement declares an array of integers containing five elements. The compiler determines the size of the array based on the number of initializers specified in curly braces. When the JVM loads this code in memory, it will initialize those memory locations allocated to the array with the values specified in the curly braces.

This type of initialization is sometimes called aggregate initialization and is a much safer method of initializing an array. If we perform runtime initialization, as discussed earlier, the code is error-prone because we might accidentally specify a wrong value for the index while modifying an element. Using an array literal centralizes the entire code for initialization, and adding/removing elements is error-free. For example, to add one more element, we just need to write a new initializer in the list, and to remove an element, you simply delete one from the list. This cannot result in an array out-of-bounds error and is therefore safer to use.

It is also important to understand that the Java Virtual Machine architecture does not support any kind of efficient array initialization. As a matter of fact, array literals are created and initialized when the program is run, not when the program is compiled. For example, consider our earlier declaration of an array literal:

int[] integerNumbers = {25, 12, 39, 2, 198};

This gets compiled into the equivalent of the following code:

int[]integerNumbers = new int[5];


integerNumbers [0] = 15;

integerNumbers [1] = 2;

integerNumbers [2] = 9;

integerNumbers [3] = 200;

integerNumbers [4] = 18;



Thus, array literals are just shorthand for writing the runtime initialization code discussed earlier.

Leave Comment