Objects.isNull is intended for use within Java 8 lambda filtering.

It's much easier and clearer to write:

.stream().filter(Objects::isNull) 

than to write:

.stream().filter(x -> x == null).  

Within an if statement, however, either will work. The use of == null is probably easier to read but in the end it will boil down to a style preference.

Answer from Craig Taylor on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Objects.html
Objects (Java Platform SE 8 )
October 20, 2025 - This method exists to be used as ... <T> T requireNonNull(T obj, Supplier<String> messageSupplier) Checks that the specified object reference is not null and throws a customized NullPointerException if it is....
🌐
Educative
educative.io › answers › what-is-objectsnonnull-in-java
What is Objects.nonNull in Java?
The nonNull method is a static method of the Objects class in Java that checks whether the input object reference supplied to it is non-null or not.
🌐
Sonar Community
community.sonarsource.com › rules and languages › new rules / language support
Set usage of Objects.isNull and Objects.nonNull as code smells - New rules / language support - Sonar Community
February 16, 2022 - While using this method does not create any problem, I consider it a code smell because this method was designed to be used along with lambdas and not for other usages Description of the method from the java docs : API Note: This method exists to be used as a Predicate , filter(Objects::isNull) snippet of Noncompliant Code if (Objects.isNull(object)) { } snippet of Compliant Code (fixing the above noncompliant code) if (object == null) { } e...
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.util.objects.nonnull
Objects.NonNull(Object) Method (Java.Util) | Microsoft Learn
[<Android.Runtime.Register("nonNull", "(Ljava/lang/Object;)Z", "", ApiSince=24)>] static member NonNull : Java.Lang.Object -> bool · obj · Object · a reference to be checked against null · Boolean · true if the provided reference is non-null otherwise false ·
🌐
OpenRewrite
docs.openrewrite.org › recipe catalog › java › transform calls to `objects.isnull(..)` and `objects.nonnull(..)`
Transform calls to Objects.isNull(..) and Objects.nonNull(..) | OpenRewrite Docs
Replace calls to Objects.isNull(..) and Objects.nonNull(..) with a simple null check. Using these methods outside of stream predicates is not idiomatic. GitHub: RemoveObjectsIsNull.java, Issue Tracker, Maven Central
Published   3 days ago
Find elsewhere
🌐
Baeldung
baeldung.com › home › java › core java › guide to objects.requirenonnull() in java
Guide to Objects.requireNonNull() in Java | Baeldung
December 29, 2024 - In this tutorial, we’ll look at the latter – a flexible, built-in solution Java offers with the Objects.requireNonNull() methods. To briefly review, many alternatives are available to avoid manual null checks. Instead of wrapping our code with if statements, which could be error-prone and time-consuming, we can choose from various libraries. Spring, Lombok (@NonNull), and Uber’s NullAway are just a few of them.
🌐
Oracle
docs.oracle.com › javase › 10 › docs › api › java › util › Objects.html
Objects (Java SE 10 & JDK 10 )
This method exists to be used as a Predicate, filter(Objects::nonNull) Parameters: obj - a reference to be checked against null · Returns: true if the provided reference is non-null otherwise false · Since: 1.8 · See Also: Predicate · public static <T> T requireNonNullElse​(T obj, T defaultObj) Returns the first argument if it is non-null and otherwise returns the non-null second argument.
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.base › java › util › Objects.html
Objects (Java SE 21 & JDK 21)
January 20, 2026 - (Object obj) Returns true if the provided reference is null otherwise returns false. static boolean · nonNull · (Object obj) Returns true if the provided reference is non-null otherwise returns false. static <T> T · requireNonNull · (T obj) Checks that the specified object reference is not null.
🌐
Medium
medium.com › @AlexanderObregon › javas-objects-requirenonnull-method-explained-8a616d001d04
Java’s Objects.requireNonNull() Method Explained | Medium
October 14, 2024 - The Objects.requireNonNull() method is part of the java.util.Objects class, introduced in Java 7. Its primary purpose is to check whether an object reference is null and, if it is, immediately throw a NullPointerException.
🌐
Tabnine
tabnine.com › home page › code › java › java.util.objects
java.util.Objects.nonNull java code examples | Tabnine
* * @param nowSecs current time in seconds */ public Set<String> getAliveIds(int nowSecs) { return SupervisorUtils.readWorkerHeartbeats(stormConf).entrySet().stream() .filter(entry -> Objects.nonNull(entry.getValue()) && !SupervisorUtils.isWorkerHbTimedOut(nowSecs, entry.getValue(), stormConf)) .map(Map.Entry::getKey) .collect(toCollection(TreeSet::new)); }
Top answer
1 of 1
44

As you noted, the semantics of the lambda x -> x != null and the method reference Objects::nonNull are virtually identical. I'm hard-pressed to think of any actual observable difference, short of digging into the class using reflection, or something like that.

There is a small space advantage to using the method reference over the lambda. With the lambda, the code of the lambda is compiled into a private static method of the containing class, and the lambda metafactory is then called with a reference to this static method. In the method reference case, the method already exists in the java.util.Objects class, so the lambda metafactory is call with a reference to the existing method. This results in a moderate space savings.

Consider these small classes:

class LM { // lambda
    static Predicate<Object> a = x -> x != null;
}

class MR { // method reference
    static Predicate<Object> a = Objects::nonNull;
}

(Interested readers should run javap -private -cp classes -c -v <class> to view detailed differences between the way these are compiled.)

This results in 1,094 bytes for the lambda case and 989 bytes for the method reference case. (Javac 1.8.0_11.) This isn't a huge difference, but if your programs are likely to have large numbers of lambdas like this, you might consider the space savings resulting from using method references.

In addition, it is more likely that a method reference could be JIT-compiled and inlined than the lambda, since the method reference is probably used a lot more. This might result in a tiny performance improvement. It seems unlikely this would make a practical difference, though.

Although you specifically said "Other than code style..." this really is mostly about style. These small methods were specifically added to the APIs so that programmers could use names instead of inline lambdas. This often improves the understandability of the code. Another point is that a method reference often has explicit type information that can help out in difficult type inference cases, such as nested Comparators. (This doesn't really apply to Objects::nonNull though.) Adding a cast or explicitly-typed lambda parameters adds a lot of clutter, so in these cases, method references are a clear win.

🌐
AlphaCodingSkills
alphacodingskills.com › java › notes › java-objects-nonnull.php
Java Objects nonNull() Method - AlphaCodingSkills
The java.util.Objects.nonNull() method returns true if the provided reference is non-null otherwise returns false. public static boolean nonNull(Object obj) Returns true if the provided reference is non-null otherwise false · NA · In the example below, the java.util.Objects.nonNull() method ...
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › util › Objects.html
Objects (Java SE 11 & JDK 11 )
October 20, 2025 - This method exists to be used as a Predicate, filter(Objects::nonNull) Parameters: obj - a reference to be checked against null · Returns: true if the provided reference is non-null otherwise false · Since: 1.8 · See Also: Predicate · public static <T> T requireNonNullElse​(T obj, T defaultObj) Returns the first argument if it is non-null and otherwise returns the non-null second argument.
🌐
Oracle
docs.oracle.com › javase › 9 › docs › api › java › util › Objects.html
Objects (Java SE 9 & JDK 9 )
This method exists to be used as a Predicate, filter(Objects::nonNull) Parameters: obj - a reference to be checked against null · Returns: true if the provided reference is non-null otherwise false · Since: 1.8 · See Also: Predicate · public static <T> T requireNonNullElse​(T obj, T defaultObj) Returns the first argument if it is non-null and otherwise returns the non-null second argument.
🌐
Apache Commons
commons.apache.org › proper › commons-lang › apidocs › org › apache › commons › lang3 › ObjectUtils.html
ObjectUtils (Apache Commons Lang 3.20.0 API)
This class tries to handle null input gracefully. An exception will generally not be thrown for a null input. Each method documents its behavior in more detail · For example, in a HashMap the HashMap.get(Object) method returns null if the Map contains null or if there is no matching key.
🌐
Sonar Community
community.sonarsource.com › rules and languages › report false-positive / false-negative...
Not validating Objects.nonNull - Report False-positive / False-negative... - Sonar Community
May 30, 2019 - SonarQube Version 6.7.1 Java 1.8 For below Code sonar raise bug: A “NullPointerException” could be thrown; “testObject” is nullable here. But we are checking non null condition in if clause. if (Objects.nonNull(testObject)) { testObject.getId(); } If we change if clause if(testObject != null) then sonarqube will not show this issue.