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:
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:
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.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:
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.