This post will discuss how to declare an empty array in Java.

1. Array Initializer

To create an empty array, you can use an array initializer. The length of the array is equal to the number of items enclosed within the braces of the array initializer. Java allows an empty array initializer, in which case the array is said to be empty.

 
The following code creates an array of zero length using an empty array initializer. Note that once an array is created, its length never changes. If you’re looking for the resizable arrays, use an ArrayList instead.

Download  Run Code

Output:

[]

 
You can also use an array creation expression to create an empty array.

Download  Run Code

Output:

[]

 
Note that an array initializer may be specified as part of the array creation expression:

Download  Run Code

Output:

[]

2. Create a 2D Array

You can declare a two-dimensional array in Java using a similar syntax. The following code creates a two-dimensional integer array of length 0, whose element is int[].

Download  Run Code

Output:

[]

 
It is also permissible to create an empty two-dimensional array containing non-empty arrays. For example, the following code creates an empty array of int[1] elements:

Download  Run Code

Output:

[]

That’s all about declaring an empty array in Java.