Table of Contents
In this post, we will see how to check if array is empty in Java.
The arrays are a data structure that facilitates storing multiple instances of data together in contiguous memory locations.
This article discusses different cases when an array is considered to be empty in Java. It also discusses methods to check, for different cases, if array is empty in Java.
Check if the Array Is Empty in Java
There are three different cases when Java considers an array to be empty. These are given as follows.
- The array variable has the null reference.
- The array does not contain any element.
- The array has only null elements.
Let us understand each of these cases one by one.
The Array Variable Has the Null Reference
In this case, an array variable actually stores a null reference, therefore, storing virtually nothing. This case can arise from two different possibilities as given below.
- When the array is not instantiated, the array variable has a null reference making it an empty array. For example,
123int [] arr; - When you explicitly assign null to the array variable. For example,
123int [] arr = null;
Let us see the code to check this condition for Java arrays.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package java2blog; public class NullArrayExample { int [] arr; int [] arr2 = null; public static void main(String [] args) { NullArrayExample obj = new NullArrayExample(); if(obj.arr==null) System.out.println("arr is empty (null)."); if(obj.arr2 == null) System.out.println("arr2 is empty (null)."); } } |
Output:
arr2 is empty (null).
The Array Does Not Contain Any Element
When you declare an array as well as instantiate it, the memory is assigned to it. The array variable stores reference to the assigned memory.
However, if you instantiate the array with the size of the array as zero, it will not store any elements, therefore, making it an empty array.
To check if the array is empty in this case,
- Check the length of the array using the ‘length’ variable.
- The Java array object has a variable named ‘length’ that stores the number of elements in the array.
- If the length is zero, the array is empty.
Let us see an example that implements the steps given above.
1 2 3 4 5 6 7 8 9 10 11 12 |
package java2blog; public class NullArrayExample { public static void main(String [] args) { int [] emptyArr = new int[0]; if(emptyArr.length == 0) System.out.println("The array is empty"); } } |
Output:
The Array Has Only Null Elements
Java initializes all the arrays of non-primitive types with null values at the time of instantiating the array. Therefore, if you do not assign non-null elements to the array, it will continue to contain the null values for all elements.
Alternatively, you can manually assign the null values to all of the array elements. In both cases, Java considers the array to be empty.
To check if the array is empty in this case,
- Initialize a boolean flag variable to true.
- Loop through all elements of the array.
- Check each element for a null value.
- If the current element is not null, change the value of the flag variable to false and break from the loop.
- Otherwise, move to the next element.
- After the loop terminates, check the flag variable.
- If the flag is true, the array is empty.
- Otherwise, the array is not empty.
Let us see the implementation of these steps in code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
package java2blog; public class NullArrayExample { public static void main(String [] args) { String [] arr = new String[10]; boolean allNull = true; for(int i=0;i<arr.length;i++) { if(arr[i] != null) { allNull=false; break; } } if(allNull) System.out.println("The array is empty!"); else System.out.println("The array is not empty!"); } } |
Output:
Note that the code declares a String array rather than an integer array as in previous cases. This is because the arrays with primitive data types are not initialized with null values by Java. For example, the integer array is initialized with zeros.
On the other hand, String is a class in Java, therefore, the String array is initialized with null values.
Further reading:
Using the Java Library to Check if the Array Is Empty in Java
Starting with the Java 8 version, Java provides library methods to perform a check on all array elements. Therefore, we can use these methods to check the condition of all null values in the array.
The java.util.Arrays
class defines the stream() method that can be used to call the allMatch()
method.
The stream()
method returns a stream created using the array passed to it as a parameter. Let us see the definition of the stream()
method.
1 2 3 |
public static Stream stream(T[] array) |
The allMatch()
method accepts a predicate (a condition to be checked) and returns a boolean value. The allMatch()
method returns a boolean true if the condition is met on all the array values. Otherwise, it returns a boolean false. Let us see the definition of the allMatch()
method.
1 2 3 |
boolean allMatch(Predicate<? super T> predicate) |
Note that using this method you can only check the condition of the empty array where all elements of the array are null. Let us see the example in code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package java2blog; import java.util.Arrays; import java.util.Objects; public class NullArrayJava { public static void main(String [] args) { String [] arr = new String [10]; boolean isNull = Arrays.stream(arr).allMatch(Objects::isNull); if(isNull) System.out.println("The array is empty"); else System.out.println("The array is not empty"); } } |
The code passes the isNull
predicate condition of the Objects class to the allMatch()
method. It means that the allMatch()
method returns true if all the elements of the array have null values.
Output:
Using Apache Commons Library to Check if the Array Is Empty in Java
The Apache commons library provides the isEmpty()
method in the ArrayUtils
class. This method can be used to check if the array is empty in Java.
You need to add below dependency to get Apache commons jar.
1 2 3 4 5 6 7 |
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.9</version> </dependency> |
Let us see the definition of the isEmpty()
method.
1 2 3 |
public static boolean isEmpty(Object[] array) |
This method checks if the array passed to it as a parameter is empty or null. It returns a boolean true if an array is empty or null. Otherwise, it returns false.
Let us see the example demonstrating its use in the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
package org.arpit.java2blog; import org.apache.commons.lang3.*; public class NullArrayJava { public static void main(String [] args) { String [] arr = new String[0]; boolean isArrEmpty = ArrayUtils.isEmpty(arr); if(isArrEmpty) System.out.println("The array 1 is empty"); else System.out.println("The array 1 is not empty"); String [] arr2 = null; boolean isArrNull = ArrayUtils.isEmpty(arr2); if(isArrNull) System.out.println("The array 2 is Null"); else System.out.println("The array 2 is not Null"); String [] arr3 = new String[10]; boolean isArrEmpty2 = ArrayUtils.isEmpty(arr3); if(isArrEmpty2) System.out.println("The array 3 is empty"); else System.out.println("The array 3 is not empty"); } } |
Output:
The array 2 is Null
The array 3 is not empty
You should note that this method only checks the following conditions.
- The array variable has a null reference.
- The array is of zero length.
It can not check if the array has all null values.
Conclusion
While working with arrays, especially in the cases where arrays are passed as parameters to methods, you should perform the checks for an empty array. This helps in preventing the unexpected failures of the program.
Apart from the direct methods, you can use the libraries to check the cases of the empty array in Java. However, you should be careful while using library methods. This is because, as we have discussed, both of the library methods work only for a subset of cases of an empty array.
Hope you have enjoyed reading the article. Stay tuned for more such articles.
Happy Learning!