Java

How to Initialize an Empty Array in Java

In Java, an array is automatically considered an object, and memory is allocated to them dynamically. Array declaration, initialization, and instantiation are three different things in Java. Array declaration means declaring an array of any data type with a specific name which tells the compiler that there is an array. Array instantiation means instantiating an object of an array, while initialization means assigning values to the array.

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:

datatype arrayName [];

For example:

int arr[];

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:

arrayName = new datatype [size];

For example:

arr = new int [10];

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:

arrayName [index] = value;

For example:

arr [0] = 3;
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

data-type[] arrayName = new data-type[];

OR

data-type arrayName [] = new data-type[];

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”:

int arr[]= new int[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:

System.out.println("Length of Array is " + arr.length);

We will print the array “arr” by using a “for” loop. As we have not initialized array, so currently the array comprises garbage values:

for(int i = 0; i <5; i++)
{
        System.out.print(arr[i]);
}

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:

for(int i=0;i<5;i++)
 {
      arr[i] = i+1;
      System.out.println("The value at index "+i+" is: "+arr[i]);
 }

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.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.