This post will illustrate the method to initialize an empty array in Java.
How to Initialize an Empty Array in Java?
Empty array initialization refers to the process of assigning values by declaring an array of a specific size without giving it any values. The array will, by default, contain garbage values, called random values, if it is not initialized with any data.
Let’s first understand the difference between declaration, instantiation, and initialization with examples.
Array Declaration
An array is simply a data structure that may hold a number of elements next to one another:
For example:
Here, “int” is the data type of an array, and the “arr” is the name of the array.
Array Instantiation
When we say “instantiate an array“, it means that we are giving that array some space in memory. You may know that in Java, the “new” keyword is used to instantiate objects as follows:
For example:
Here, “10” is the size of an array named “arr” which will now have ten consecutive empty memory blocks.
Array Initialization
Array initialization means assigning the values and storing it the specified indexes:
For example:
arr [1] = 7;
……
Let’s initialize the empty array with examples.
How to Initialize an Empty Array in Java by Using the “new” Keyword?
This section will first declare and instantiate an array without assigning any value (creating an empty array). Then, we will initialize this empty array with some specific values.
There are two syntax formats that can be utilized for declaring and instantiating an empty array.
Syntax
OR
The size or the length of the array is defined in rectangular brackets on the right side. The size tells the compiler how much space should be reserved for the array.
Example 1: Declare an Empty Array
In this example, we will declare and instantiate an integer type empty array of size “5”:
This array reserves five empty blocks in the memory that will be used later. At the time of an empty array declaration, the array is typically initialized with garbage values of the same data type that will be overwritten when we assign the values to it.
Then, we will verify the array length using the “arr.length” property:
We will print the array “arr” by using a “for” loop. As we have not initialized array, so currently the array comprises garbage values:
Since “arr” is not initialized, its five memory blocks comprise “0” as the garbage value:
Example 2: Initialize an Empty Array
In this example, we will initialize the above created empty array by assigning values to it using “for” loop:
The output shows the length of the array that is “5” and the predefined values:
We presented the method to initialize an empty array in Java.
Conclusion
To initialize an empty array in Java, you must first declare and instantiate an array with some specific size. When you declare and instantiate an array, the array will reserve some space in memory based on the defined size. This array will, by default, contain garbage values. Initializing an empty array means assigning values to the empty declared array. This post illustrated the methods to initialize an empty array in Java.