VOOZH about

URL: https://www.geeksforgeeks.org/java/how-to-check-if-an-array-is-empty-or-not-in-java/

⇱ How to Check if an Array is Empty or Not in Java? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Check if an Array is Empty or Not in Java?

Last Updated : 23 Jul, 2025

In Java, arrays do not have a built-in method like isEmpty() to check if they are empty. So, we need to use other ways to check if an array is empty in Java.

Example: The simplest way to determine if an array is empty is by evaluating its length or verifying if it is null. This approach ensures our code handles both uninitialized and empty arrays effectively.


Output
The array is empty.

Explanation: The array == null checks if the array reference is uninitialized or null and the array.length == 0checks if the array has no elements.

Other Methods to Check Empty Arrays

1. Using Utility Method

If we need to check arrays frequently, we can create a reusable utility method to simplify array emptiness check.


Output
true

Note: This code is reusable and provides a clean, clear way to check if an array is empty in Java.

2. Using Java Optional

Java's Optional class provides a clean and functional approach to check if an array is empty.


Output
true

Explanation: Here, we have used Optional.ofNullable to safely handle potential null arrays, avoiding NullPointerException. It checks if the array's length is 0 and prints true if the array is empty or uninitialized.

Comment