isEmpty
is a static method of the ArrayUtils
class that checks whether the given array is empty or null.
ArrayUtils
is defined in the Apache Commons Lang package. Apache Commons Lang can be added to the Maven project by adding the following dependency to the pom.xml
file.
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
For other versions of the commons-lang package, refer to the Maven Repository.
ArrayUtils
You can import the ArrayUtils
class as follows:
import org.apache.commons.lang3.ArrayUtils;
public static boolean isEmpty(final int[] array)
final int[] array
: This parameter is an array to check for emptiness.
This method returns true
if the array is empty or null
, and returns false
otherwise.
array = [1,2,3,4,5]
Applying the isEmpty
function will result in false
, as the array is not empty.
array = []
Applying the isEmpty
function will result in true
, as the array is empty.
import org.apache.commons.lang3.ArrayUtils;public class Main {public static void main(String[] args) {int[] array = {1,2,3,4,5,2,3,2,2,4};boolean check = ArrayUtils.isEmpty(array);System.out.println("Array is empty - " + check);int[] array1 = new int[0];check = ArrayUtils.isEmpty(array1);System.out.println("Array1 is empty - " + check);}}
Array is empty - false
Array1 is empty - true