Check and Declare Empty Array in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Overview

An empty array in Java refers to an array in Java with the length 0 or an array with no elements.

To check if an array is empty in Java, it should satisfy one of the following conditions:

-It should not contain any element, i.e. the size of the array should be 0. -It should consist only of null elements. In further sections, we will learn how to declare a Java empty array and how to check if an array is empty in Java.

How to Check if An Array is Empty in Java?

Using Null Check

In this section, we will learn how to check for a java empty array using the NULL property. An array can be considered an empty array java if the array displays the value NULL.

Code:

Output:

Explanation of the Example:

In the above example, we have declared an array dates and assigned the values null. The 'if condition' will compare the dates with null. It if equals to null then our program will display "The input array is an empty array" otherwise it will display "The input array is not an empty array".

Using Length Property

In this section, we will learn how to check for a java empty array using the length property. The length of an array can be used to determine whether it is an empty array or not. If the length of an array is 0 then the given array is an empty array.

Code:

Output:

Explanation of the example:

In the above example, we have declared an array dates. The dates.length will obtain the size of the array. Iif it equals 0 then our program will display "The input array is an empty array" otherwise it will display "The input array is not an empty array".

Using Null Check on Elements

In this section, we will learn how to check for a java empty array using the null check on elements property. An array in java is said to be empty if all the elements present in the array are null. Thus we will traverse through the array and if we encounter a non-null element then the array is not empty otherwise it is empty.

Code:

Output:

Explanation of the example:

In the above example, we have declared an array dates and assigned the value null to each place in the array. The for condition Date it: dates will loop through each element in the dates and compare it with null. If any item does not equal to null then the boolean value isNotEmpty will be assigned true and we will break the loop. If isNotEmpty is false then our program will display "The input array is an empty array" otherwise it will display "The input array is not an empty array".

Using the Java Library

In this section, we will learn how to check for a java empty array using the Java Library. Java has provided library methods to check if java empty array.

To check for empty array java we need to import the java.util.Arrays class in our Java program. The java.util.Arrays class provides the stream() method which can be used to call the allMatch() method to check the condition of all null values in the array.

Code:

Output:

Explanation of the example:

In the above example, we have declared an array dates and assigned the value null to each place in the array. The allMatch method will loop through each element in the dates and compare it with null. If any item does not equal null then the boolean value isEmpty will be assigned false and we will break the loop. If isEmpty is false then our program will display "The input array is an empty array" otherwise it will display "The input array is not an empty array".

Using Apache Commons Library

In this section, we will learn how to check for a java empty array using the Apache commons library.

The isEmpty() function takes an array as a parameter. Then it will check if the parameter passed to it is null or empty. If the array passed as a parameter is null or empty then it would return a true. If the array passed as a parameter is not null or empty then it would return a false.

Note: If you're using an external library like Apache Commons Lang, you need to make sure that you have added it to your project's classpath. If the library is not available, you'll get a "class not found" error.

Code:

Output:

Explanation of the example:

In the above example, we have declared an array dates and assigned the value null to each place in the array. The ArrayUtils.isEmpty(dates) method will loop through each element in the dates and compare it with null. If any item does not equal null then the boolean value isEmpty will be assigned false and we will break the loop. If isEmpty is false then our program will display "The input array is an empty array" otherwise it will display "The input array is not an empty array".

How to Initialize an Empty Array in Java?

Using new Keyword

In this section, we will learn how to initialize empty array java using the new keyword.

To initialize an empty array java we need to use the new keyword followed by the data type of the elements the array would store. The size of this array will be 0.

code:

Output:

Using new Keyword with Predefined Size

In the previous section, we defined a java empty array of size 0. But what if we want to declare a java array of a certain size?

The new keyword can be used to initialize an array java with a predefined size. To initialize an array java with a predefined size we need to use the new keyword followed by the data type of the array and the size of the array.

Code:

Output:

Conclusion

  • An array is said to be empty in java if it has size 0.
  • An array is said to be empty in java if it displays a null value.
  • An empty array java can be initialized using the new keyword.