Java – Check if Array is Empty
There is no standard definition to define an empty array. We will assume an array is empty if
- Array is null.
- Array has no elements inside it.
- All the elements inside the array are null.
Example 1 – Check if Array is Empty using Null Check
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.");
}
}
}
Output
The array is 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.");
}
}
}
Output
The array is 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[] = {null, null, null};
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 {
public String name;
public String value;
}
Output
The array is empty.
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[] = {null, null, null};
if(isArrayEmpty(arr2)) {
System.out.println("arr2 is empty.");
} else {
System.out.println("arr2 is not empty.");
}
String arr3[] = {"apple", "banana", "cherry"};
if(isArrayEmpty(arr3)) {
System.out.println("arr3 is empty.");
} else {
System.out.println("arr3 is not empty.");
}
}
}
Output
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.