Arrays can be initialized in two ways: statically and dynamically.

Static initialization

Static initialization is when the programmer assigns values to each element of the array while initializing it, and the system determines the length of the array.

There are two ways to statically initialize an array, as shown in the following example:

Array = new int[]{1,2,3,4,5};

Int [] array = {1,2,3,4,5};

Both of the above methods implement statically initializing arrays, with curly braces containing array element values separated by commas (,). Note here that the simplified static initialization is supported only if the array initialization is performed simultaneously with the array definition. The second method is recommended for simplicity.

Dynamic initialization

Dynamic initialization means that the programmer specifies the array length when initializing the array, and the system assigns initial values to array elements.

An array is dynamically initialized as shown in the following example:

int[ ] array = new int[10]; // Dynamically initialize the array

The format in the above example allocates a block of memory for the array along with the array declaration. The length of the array is 10. Since each element is an int, the total memory occupied by the array in the above example is 10*4=40 bytes. In addition, when an array is dynamically initialized, its elements are set to default initial values based on their data types. The default value for each element in this sample group is 0,