Java

What is Objects.isNull in Java?

In Java, an object reference of any type can be assigned a null value to indicate that it does nothing. Static and instance members of any uninitialized reference type receive a null value from the compiler. To check whether a Java object is null or not, we can either use the “isNull()” method of the Object class or the comparison operator.

This write-up demonstrates the usage of the Objects.isNull() method in Java.

What is Objects.isNull() in Java?

In Java, “object.isNull” is a method used by the object class to determine if the input object reference passed to it is null or not. If the object is empty, this method returns “true” as the value. This method returns a “true” value if the object is null. However, if the passed object is not null, then it will output “false”.

Example 1: How to Use “Objects.isNull()” By Passing Not Null Object?

To use the “Objects.isNull” in Java, first of all, import the “java.util.Objects” library:

import java.util.Objects;

Next, initialize the variable with a particular name along with the class name. After that, utilize the “Objects.isNull()” method and pass a value. To do so, we will pass the created “abc” string. Lastly, use the “System.out.println()” method to display the output:

String abc = "stringTest";

boolean bl = Objects.isNull(abc);

System.out.println(bl);

The “Objects.isNull()” method has returned “false” because the passed string was not null:

Example 2: How to Use “Objects.isNull” By Passing Null Object?

To use “Objects.isNull” by passing a null object, there are multiple libraries that can need to be imported:

import java.util.Arrays;

import java.util.List;

import java.util.Objects;

Here:

  • java.util.Arrays” is used to represent arrays as a list.
  • java.util.List” is a child interface of Collection. It is a set of objects that may hold duplicate data in an organized manner.
  • java.util.Objects” class is part of the java.util package. The Objects class has a private function Object() and is a final class.

Now, add the following code where:

  • The “Arrays.asList()” method of the “java.util.Arrays” class creates an array containing objects
  • list.stream()” the stream is a sequence of objects that supports various methods that can be pipe-lined to produce the desired result. In our case, we have utilized the “Objects::isNull” method to check if the list contains any null object.
  • System.out.println()” is utilized for showing output on the console:
List<String> list = Arrays.asList("x", "y", null, null);

boolean bl = list.stream().anyMatch(Objects::isNull);

System.out.println(bl);

Output

The given output signifies that null objects exist in the list.

Conclusion

In Java, “object.isNull” is a method used by the object class to determine if the input object reference passed to it is null or not. If the object is empty, this method returns “true” as the value. However, if the passed object is not null, then the method returns false. This write-up has stated the method to use the “Objects.isNull” in Java.

About the author

Hafsa Javed