Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is ObjectUtils.allNull in Java?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

allNull() is a staticdescribes the methods in Java that can be called without creating an object of the class method of the ObjectUtils class that is used to check if all the values in the array point to a null reference.

  • The method returns false if any value in the array of values is not null.
  • The method returns true if all the values in the passed array are null or the array is null or empty.

How to import ObjectUtils

The definition of ObjectUtils can be found in the Apache Commons Lang package, which we can add 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.

You can import the ObjectUtils class as follows:

import org.apache.commons.lang3.ObjectUtils;

Syntax

public static boolean allNull( Object... values)

Parameters

values: The list of objects to test.

Return value

The allNull() method returns true if all of the objects in the list are null. Otherwise, it returns false.

Code

import org.apache.commons.lang3.ObjectUtils;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
Object[] objects = {"hi", 3, 3.24};
System.out.printf("The output of ObjectUtils.allNull() for the string - '%s' is %s", Arrays.toString(objects), ObjectUtils.allNull(objects));
System.out.println();
objects = null;
System.out.printf("The output of ObjectUtils.allNull() for the string - '%s' is %s", Arrays.toString(objects), ObjectUtils.allNull(objects));
System.out.println();
objects = new Object[]{null, null, null};
System.out.printf("The output of ObjectUtils.allNull() for the string - '%s' is %s", Arrays.toString(objects), ObjectUtils.allNull(objects));
System.out.println();
objects = new Object[]{1.2, null, null};
System.out.printf("The output of ObjectUtils.allNull() for the string - '%s' is %s", Arrays.toString(objects), ObjectUtils.allNull(objects));
System.out.println();
objects = new Object[0];
System.out.printf("The output of ObjectUtils.allNull() for the string - '%s' is %s", Arrays.toString(objects), ObjectUtils.allNull(objects));
System.out.println();
}
}

Output

The output of the code is as follows:

The output of ObjectUtils.allNull() for the string - '[hi, 3, 3.24]' is false

The output of ObjectUtils.allNull() for the string - 'null' is true

The output of ObjectUtils.allNull() for the string - '[null, null, null]' is true

The output of ObjectUtils.allNull() for the string - '[1.2, null, null]' is false

The output of ObjectUtils.allNull() for the string - '[]' is true

Explanation

Example 1

  • objects = ["hi", 3, 3.24]

The method returns false because none of the objects in the list are null.

Example 2

  • objects = null

The method returns true because the array/list of objects points to null.

Example 3

  • objects = [null, null, null]

The method returns true because all the values in the array/list of objects point to null.

Example 4

  • objects = [1.2, null, null]

The method returns false because at least one of the objects in the list is not null.

Example 5

  • objects = []

The method returns true because the array/list of objects is empty.

RELATED TAGS

java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring