Javatpoint Logo
Javatpoint Logo

How to Declare an Empty Array in Java?

In Java, we use an array to store elements of the same data type. Sometimes it is required to declare an empty array or to generate an array without initializing it with any values. In this section, we will learn how to declare an empty array in Java.

Method 1: Initialization without Declaration

Declaring an empty array in Java without initializing it with any values is the easiest method to do it.

An array of integers called emptyIntArray is shown in this example; it is declared without being initialised. When necessary, you can subsequently allocate a new array to the first null array.

By doing this, emptyIntArray is initialised as a five-dimensional integer array. By default, the values of the array items are set to zero for integers.

Method 2: Using an Empty Array as the Initialization

Another way to define an empty array is to initialize it with a certain type of empty array.

Method 3: Using Arrays.copyOf()

Using the Arrays.copyOf() function from the java.util package provides an additional option for creating an empty array. We can use an empty array of the required type to produce an empty array:

This example uses Arrays.copyOf() to initialise a double array called emptyDoubleArray. An empty array of doubles is the first parameter, and the required length, in this instance zero, is the second.

Method 4: Using Collections.emptyList

Use Collections.emptyList() from the java.util package if you are dealing with lists and require an empty array. An immutable empty list is returned by this function.This we can convert into an array.

Method 5: Using Arrays Class

Another way to build an empty array is to use the copyOfRange() method. By giving an empty range, you essentially generate an empty array. The function lets you provide a range of elements to copy:

The example uses Arrays.copyOfRange to initialise a char array called emptyCharArray. The range (from 0 to 0) essentially generates an empty array, while the source array is an empty array of characters.

Method 6: Using Stream API

The toArray function may be used to construct an empty array. Java 8 added the Stream API:

The long array in this example, emptyLongArray, was made with the Stream API. An empty stream of long values is created by the LongStream.empty() function, and it is transformed into an array using the toArray method.

Method 7: Using Apache Commons Lang

The ArrayUtils class in the Apache Commons Lang library may be utilised to generate an empty array:

EmptyArray.java

Output:

Length of emptyIntArray: 5
Length of emptyStringArray: 0
Length of emptyDoubleArray: 0
Length of emptyArrayFromList: 0
Length of emptyCharArray: 0
Length of emptyLongArray: 0

Conclusion

There are several ways to declare an empty array in Java, each having specific applications. You have options depending on your needs, like utilising Collections.emptyList(), Arrays.copyOf, simple declaration, or initialization with an empty array. Select the Java method for generating and manipulating empty arrays that best meet your needs.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA