Angle Bracket <> in Java with Examples Different types of Recursions in Java Java Lambda Filter Example Java Program for Shopping Bill Pythagorean Triplet with Given Sum Using Single Loop in Java TemporalAdjusters lastInMonth() method in Java with Examples ZIP API in Java Atomic reference in Java Digit Extraction in Java DRY (Don't Repeat Yourself) Principles in Java with Examples Empty array in Java Is main method compulsory in Java? Java I/O Operation - Wrapper Class vs Primitive Class Variables Java Program to Find the Perimeter of a Rectangle Java Thread Priority Java Type Erasure Need of Concurrent Collections in Java Nested ArrayList in Java Print Odd and Even Numbers by Two Threads in Java Square pattern in Java TemporalAdjusters next() method in Java with Examples What does start() function do in multithreading in Java Convert Number to Words Problem in Java Detect And Remove Cycle in a Single Linked List in Java Evolution of Interfaces in Java How to pad a String in Java Implementing Patricia Trie in Java Java Program to Find the Most Repeated Word in a Text File java.util.UUID class in Java ReadWriteLock Interface in Java Reference Data Types in Java Sort An Array According to The Count of Set Bits in Java Alternate Vowel and Consonant string in Java Built-in Exceptions in Java with Examples Capture the Pawns Problem in Java Collections.shuffle() Method in Java with Examples JDBC MySQL Localhost 3306 Alternate Vowel and Consonant string in Java Built-in Exceptions in Java with Examples Capture the Pawns Problem in Java Collections.shuffle() Method in Java with Examples Convert Number to Words Problem in Java Detect And Remove Cycle in a Single Linked List in Java Evolution of Interfaces in Java How to pad a String in Java Implementing Patricia Trie in Java Java Program to Find the Most Repeated Word in a Text File java.util.UUID class in Java ReadWriteLock Interface in Java Reference Data Types in Java Sort An Array According to The Count of Set Bits in Java JDBC MySQL Localhost 3306 Adding a Character as Thousands Separator to Given Number in Java Circular Primes in Java Equilibrium Index Problem in Java Java String regionMatches() Method with Examples LFU Cache Problem in Java Longest Repeating and Non-Overlapping Substring in Java Prefix Suffix Search in Java Product Array Puzzle Problem in Java Russian Doll Envelopes Problem in Java Second Most Repeated Word in a Sequence in Java Special Two-Digit Numbers in a Binary Search Tree in Java Swap Corner Words and Reverse Middle Characters in Java Toggle K bits Problem in Java Upside Down Binary Tree in Java Verbal Arithmetic Puzzle Problem in Java Insert a String into another String in Java Print Shortest Path to Print a String on Screen in Java Search Insert Position in Java BST Sequences in Java Burrows - Wheeler Data Transform Algorithm in Java Convert BST to Min Heap in Java Fibonacci Sum in Java Get name of current method being executed in Java

Empty array in Java

An empty array in Java is one that either has no elements or has all its elements are null. The new keyword in Java can be used to declare an empty array. Java allows us to determine whether an array is empty or not by determining whether its length is zero or whether all its entries have null values. Java also offers a variety of libraries to check Java empty arrays.

Generally, an empty array should satisfy the following two conditions

  1. The length of the array should be Zero (array_name.length=0).
  2. The array should not contain any elements.

When an array satisfies any of the above conditions, it is referred to as an empty array.

Checking whether an array is empty or not.

There are several methods to check whether an array is empty or not. Let us try to understand such methods.

Approach 1: Using 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 empty. (array_name.length=0)

Let us try to understand using a Java program.

Filename: Emptyarray1.java

public class Emptyarray1

{

public static void main(String[] args)

{

int array[] = {}; // Initialized the array no values.

if (array.length == 0)

{

System.out.println("The given array is an empty array");

}

else

{

System.out.println("The given array is not an empty array");

}

}

}

Output:

The given array is an empty array

Explanation: In the above program, we initialized the array with zero values. As the array contains no values, it returns array.length as 0, which is an empty array.

Approach 2: Null Check

Anarray can be considered an empty array java if the array shows the value NULL (array_name==null).

Filename: Emptyarray2.java

public class Emptyarray2

{

public static void main(String[] args) {

//Assigning the value null.

int array[] = null;

if (array == null)

{

System.out.println("The given array is an empty array");

}

else

{

System.out.println("The given array is not an empty array");

}

}

}

Output:

The given array is an empty array

Explanation: In the above program, we Initialized the given array with null. if condition will compare the given array with null. As the condition is true, it prints, "The given array is an empty array."

Approach 3: Using the Java library

Java programs have to include the java.util.Arrays class in order to check for empty arrays. The stream() method of the java.util.Arrays class can be used to use the allMatch() method to determine whether all of the array's null values are present.

Filename: Emptyarray3.java

import java.sql.Date;

import java.util.Arrays;

import java.util.Objects;

public class Emptyarray3 {

public static void main(String[] args) {

//assigning the value null.

Date dates[] =

{

null,

null,

null

};

boolean isEmpty = Arrays.stream(dates).allMatch(Objects::isNull);

if (isEmpty == true)

{

System.out.println("The given array is an empty array");

}

else

{

System.out.println("The given array is not an empty array");

}

}

}

Output:

 The given array is an empty array

Explanation: In the example above, we declared an array of array and gave each position in the array the value null. Each element in the array will be looped through by the allMatch function and compared to null. The boolean value isEmpty will be set to false, and the loop will be broken if any item does not equal null. It will show "The input array is an empty array" if isEmpty is false. Otherwise, it will show "The input array is not an empty array."

Approach 4: Null Check on Elements

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 get a non-null element, then the array is not empty; otherwise, it is empty.

Filename: Emptyarray4.java

import java.util.Date;

public class Emptyarray4

{

public static void main(String[] args)

{

    //assigning the value null.

Date array[] =

{

null,

null,

null

};

boolean isNotEmpty = false;

for (Date i: array)

{

if (i != null)

{

isNotEmpty = true;

break;

}

}

if (isNotEmpty == false)

{   

System.out.println("The given array is an empty array");

}

else

{

System.out.println("The given array is not an empty array");

}

}

}

Output:

 The given array is an empty array

Explanation: In the example above, we declared an array of array and gave each position in the array the value null. Each element in the dates will be looped through and checked against null using the for-condition Date i: array. The boolean variable isNotEmpty will be set to true, and the loop will be broken if any item does not equal null. It will display "The given array is an empty array" if isNotEmpty is false. Otherwise, it will display "The given array is not an empty array."