- Java – Check if Array is Empty
- Example 2 – Check if Array is Empty using Length Property
- Example 3 – Check if Array is Empty using Null Check on Elements
- Example 4 – Check if Array is Empty
- Conclusion
- Check if Array Is Empty in Java
- Check if the Array Is Empty in Java
- The Array Variable Has the Null Reference
- The Array Does Not Contain Any Element
- The Array Has Only Null Elements
- Further reading:
- Using the Java Library to Check if the Array Is Empty in Java
- Using Apache Commons Library to Check if the Array Is Empty in Java
- Conclusion
- Check Whether an Array Is Null/Empty in Java
- Null Array in Java
- Array Contains Null Values
- Empty Array in Java
- Check Array Null Using Apache Commons Library in Java
- Check Array Null Using Java 8
- Related Article — Java Array
- How to check if an array is empty in Java?
- Check if an array is empty using Apache ArrayUtils
- Check if an array is empty using Java 8
Java – Check if Array is Empty
To check if an array is null, use equal to operator and check if array is equal to the value null.
In the following example, we will initialize an integer array with null. And then use equal to comparison operator in an If Else statement to check if array is null.
Java Program
public class ArrayExample < public static void main(String[] args) < int arr[] = null; if(arr == null) < System.out.println("The array is empty."); >else < System.out.println("The array is not empty."); >> >
Example 2 – Check if Array is Empty using Length Property
To check if an array has no elements, get length property of the array and check if the length is zero.
In the following example, we will initialize an integer array with empty array. And then use equal to comparison operator in an If Else statement to check if array length is zero.
Java Program
public class ArrayExample < public static void main(String[] args) < int arr[] = <>; if(arr.length == 0) < System.out.println("The array is empty."); >else < System.out.println("The array is not empty."); >> >
Example 3 – Check if Array is Empty using Null Check on Elements
To check if an array has all null elements, use a looping technique and check if the elements are all null.
In the following example, we will initialize an array of user defined objects of type Color with null objects. And then use For loop with an If Else statement to check if all the elements are null.
Java Program
public class ArrayExample < public static void main(String[] args) < Color colors[] = ; boolean empty = true; for(Color color: colors) < if(color != null) < empty = false; break; >> if(empty) < System.out.println("The array is empty."); >else < System.out.println("The array is not empty."); >> > class Color
Example 4 – Check if Array is Empty
In this example, we will combine all the above scenarios and define a function that checks if the array is empty.
Java Program
public class ArrayExample < public static boolean isArrayEmpty(String arr[]) < if(arr == null) < return true; >else if(arr.length == 0) < return true; >else < for(String str: arr) < if(str != null) < return false; >> return true; > > public static void main(String[] args) < String arr[] = null; if(isArrayEmpty(arr)) < System.out.println("arr is empty."); >else < System.out.println("arr is not empty."); >String arr1[] = <>; if(isArrayEmpty(arr1)) < System.out.println("arr1 is empty."); >else < System.out.println("arr1 is not empty."); >String arr2[] = ; if(isArrayEmpty(arr2)) < System.out.println("arr2 is empty."); >else < System.out.println("arr2 is not empty."); >String arr3[] = ; if(isArrayEmpty(arr3)) < System.out.println("arr3 is empty."); >else < System.out.println("arr3 is not empty."); >> >
arr is empty. arr1 is empty. arr2 is empty. arr3 is not empty.
You can change the type of parameter in the isArrayEmpty() function to suit your requirements. We have defined the function to check if a string array is empty.
Conclusion
In this Java Tutorial, we learned how to check if an array is empty in Java. We have defined what an empty array is, and based on that definition, we have given ways to check. Based on your definition of an empty array, you can make the required changes to the conditions that we have to check for an empty array.
Check if Array Is Empty in Java
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,
Let us see the code to check this condition for Java arrays.
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.
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.
- 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.
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:
Initialize empty array in java
How to declare a String array in java
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.
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.
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.
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.
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.Let us see the definition of the isEmpty() method.
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.
You should note that this method only checks the following conditions.
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.
Check Whether an Array Is Null/Empty in Java
- Null Array in Java
- Array Contains Null Values
- Empty Array in Java
- Check Array Null Using Apache Commons Library in Java
- Check Array Null Using Java 8
This tutorial introduces how to check whether an array is null or empty in Java and also lists some example codes to understand the null checking process.
Null Array in Java
In Java, an array is an object that holds similar types of data. It can be null only if it is not instantiated or points to a null reference.
In this example, we have created two arrays. The array arr is declared but not instantiated. It does not hold any data and refers to a null reference (default value) assigned by the compiler. The array arr2 is declared and explicitly assigned to null to create a null array.
We can use this example to check whether the array is null or not.
public class SimpleTesting String[] arr; String[] arr2 = null; public static void main(String[] args) SimpleTesting obj = new SimpleTesting(); if(obj.arr == null) System.out.println("The array is null"); > if(obj.arr2 == null) System.out.println("The array2 is null"); > > >
The array is null The array2 is null
Array Contains Null Values
This is the second scenario where an array contains null values. In that case, we can consider an array to be null.
Suppose, we have an array of string that can contain 5 elements. Since the array is not initialized then it holds null (default value) assigned by the compiler.
public class SimpleTesting String[] arr = new String[5]; public static void main(String[] args) boolean containNull = true; SimpleTesting obj = new SimpleTesting(); for(int i = 0; iobj.arr.length; i++) if(obj.arr[i] != null) containNull = false; break; > > if(containNull) System.out.println("Array is null"); > > >
Empty Array in Java
An array is empty only when it contains zero(0) elements and has zero length. We can test it by using the length property of the array object.
public class SimpleTesting String[] arr = new String[0]; public static void main(String[] args) SimpleTesting obj = new SimpleTesting(); if(obj.arr.length == 0) System.out.println("The array is Empty"); > > >
Check Array Null Using Apache Commons Library in Java
If you are working with Apache then use ArrayUtils class to check whether an array is empty. The ArrayUtils class provides a method isEmpty() which returns a boolean value either true or false. For more info about apache library visit here.
import org.apache.commons.lang3.ArrayUtils; public class SimpleTesting String[] arr = new String[0]; public static void main(String[] args) SimpleTesting obj = new SimpleTesting(); Boolean isEmpty = ArrayUtils.isEmpty(obj.arr); if(isEmpty) System.out.println("Array is Empty"); > > >
Check Array Null Using Java 8
If you are working with Java 8 or higher version then you can use the stream() method of Arrays class to call the allMatch() method to check whether array contains null values or not.
This is the case when array contains null values.
import java.util.Arrays; import java.util.Objects; public class SimpleTesting String[] arr = new String[10]; public static void main(String[] args) SimpleTesting obj = new SimpleTesting(); Boolean containNull = Arrays.stream(obj.arr).allMatch(Objects::nonNull); if(!containNull) System.out.println("Array is null"); > > >
Related Article — Java Array
How to check if an array is empty in Java?
In Java, we can check if an array is empty using the methods discussed below.
Check if an array is empty using Apache ArrayUtils
If the user is working with Apache, the ArrayUtils class contains the isEmpty() method that can be used to check if an array is empty.
import org.apache.commons.lang3.ArrayUtils; public class Demo < String[] arr = new String[5]; public static void main(String[] args) < Demo d = new Demo(); boolean flag = ArrayUtils.isEmpty(d.arr); if (flag) < System.out.println("The array is empty."); >> >
Check if an array is empty using Java 8
The stream() method of the Arrays class contains the allMatch() method that checks if an array contains null values or not.
import java.util.Arrays; import java.util.Objects; public class Test < static String[] arr = new String[10]; public static void main(String[] args) < boolean flag = Arrays.stream(arr).allMatch(Objects::nonNull); if (!flag) < System.out.println("The array is empty."); >> >
In this method, an array is declared and is initialized as an empty array. Then the array.length() method is used, if it is zero then the array is empty.
String arry[] = <>; if (arry.length == 0) < System.out.println("The array is empty."); >else