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


In Java, Array is an object. It is a non-primitive data type which stores values of similar data type.

As per the problem statement we have to check if an array is empty or not. An array is said to be an empty array, if the array has zero element or no element in it.

Let’s explore the article to see how it can be done by using Java programming language.

To Show You Some Instances

Instance-1

Suppose the original array is {1,3}.

After checking if it is empty or not the result will be −

Array is not empty.

Instance-2

Suppose the original array is {}.

After checking if it is empty or not the result will be −

Array is empty 

Instance-3

Suppose the original array is {1,3,4,7,8}.

After checking if it is empty or not the result will be −

Array is not empty

Algorithm

  • Step 1 − Declare and initialize an integer array.

  • Step 2 − Get the length of the array.

  • Step 3 − If length is equal to 0 then array is empty otherwise not.

  • Step 4 − Print the desired result.

Syntax

To get the length of an array (number of elements in that array), there is an inbuilt property of array i.e length.

Below refers to the syntax of it −

array.length

where, ‘array’ refers to the array reference.

Multiple Approaches

We have provided the solution in different approaches.

  • By Using Static Array and length() Method.

  • By Using User Defined Method and length() Method.

  • By Using null Check.

Let’s see the program along with its output one by one.

Approach-1: By Using Static Array and length() Method

Example

In this approach, array elements will be initialized in the program. Then as per the algorithm check if an array is empty or not.

public class Main {
   //main method
   public static void main(String[] args) {
      //Declare and initialize the array elements
      int arr[] = {1,3,4,7,8};
      
      //check if array is empty or not
      if(arr.length == 0) {
         //if length is zero then array is empty.
         System.out.println("Array is empty.");
      } else {
         //otherwise array is not empty.
         System.out.println("Array is not empty.");
      }
   }
}

Output

Array is not empty.

Approach-2: By Using User Defined Method and length() Method

Example

In this approach, array elements will be initialized in the program. Then call a user defined method by passing the array as parameter and inside method as per the algorithm check if an array is empty or not.

public class Main{
   //main method
   public static void main(String[] args) {
      //Declare and initialize the array elements
      int arr[] = {};
      
      //callin user defined method
      func(arr);   
   }
   //user defined method
   public static void func(int arr[]){
      //check if array is empty or not
      if(arr.length == 0) {
         //if length is zero then array is empty.
         System.out.println("Array is empty.");
      } 
      else {
         //otherwise the array is not empty.
         System.out.println("Array is not empty.");
      }
   }
}

Output

Array is empty.

Approach-3: By Using null Check

Example

Here array is declared as null means no element. Take an if condition and with the help of equal to operator check array is null or not.

public class Main{
   public static void main(String[] args){
      int arr[] = null;
      //check if array is equal to null or not by using equal to operator
      if(arr == null) {
         System.out.println("Empty array");
      } else {
         System.out.println("Not an empty array");
      }
   }
}

Output

Empty array

In this article, we explored how to check if an array is empty or not in Java.

Updated on: 05-Jan-2023

11K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements