. Advertisement .
..3..
. Advertisement .
..4..
It is easy to initialize an empty array in Java since this is a supported feature of this programming language. With this tutorial, we will show you how.
Initialize An Empty Array In Java
With The new Keyword
The new keyword is the most popular way to initialize an array in Java. It allows us to declare its name, the data type of elements stored in the array, and its size.
This is the standard way to declare an array: datatype [ ] arrayName = new datatype [size];
This is an example of using the new keyword to create an empty array with a length of zero:
import java.util.Arrays;
public class MyClass {
public static void main(String args[]) {
String[] array = new String[0];
System.out.println(Arrays.toString(array));
}
}
Output:
[]
The snippet above initializes a string array with the new keyword, specifying the length is zero. Remember that once an array has been created, we have no way to change this length. And since this array has no element, we can’t modify its content either.
To attest to the emptiness of this array, we can use the Array.toString()
method. It will try to convert our array to a string and print the result to the output. The returned value shows that there is nothing stored in this empty array.
This is an example of using the new keyword to create an empty array that has a length longer than zero:
public class MyClass {
public static void main(String args[]) {
String[] array = new String[5];
for (int i =0;i < 5;i++)
{
System.out.println(array[i]);
}
}
}
Output:
null
null
null
null
null
The new keyword in this program creates a string array with five elements. But it and the rest of the program don’t specify or assign values to these elements, meaning we still have an empty array.
We can verify this with a for loop that goes through and prints the content of each element of the array. As you can see from the output, all of them have the value null.
Compared to an array with the length of zero, we can update the contents of this array later, such as:
public class MyClass {
public static void main(String args[]) {
String[] array = new String[5];
array[0] = "ITTutoria";
array[1] = "Stack Overflow";
for (int i =0;i < 5;i++)
{
System.out.println(array[i]);
}
}
}
Output:
ITTutoria
Stack Overflow
null
null
null
The array is no longer empty. However, the elements we haven’t assigned any value still store the null value. We can then, for example, sort this array without the sort()
method. Check out this guide to learn more about it.
Remember that we can also use the new keyword to create empty two-dimensional arrays.
import java.util.Arrays;
public class MyClass {
public static void main(String args[]) {
int[][] array = new int[0][];
System.out.println(Arrays.toString(array));
}
}
Output:
[]
The program above creates a new 2D array of int values but only provides the length of one dimension, which is zero. The result is an empty array.
Without The new Keyword
While the new keyword is the most reliable and easiest way to create an array in Java, including empty arrays, you have some alternatives to it as well.
For starters, you can remove the new keyword from an array declaration like this:
import java.util.Arrays;
public class MyClass {
public static void main(String args[]) {
String[] array = {};
System.out.println(Arrays.toString(array));
}
}
Output:
[]
The statement above acts like a full one with the new keyword in the first section above. We don’t specify its size, but since we don’t assign any content to it, the result is still an empty array.
Conclusion
The main way to initialize an empty array in Java is to use the new keyword. However, we can eliminate it in some cases, and the resulting array is still empty.
Leave a comment