Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Java Objects.isNull: When and How to Use It

Explains Java's Objects.isNull method, when to prefer it over == null, and where Objects.nonNull and Objects.requireNonNull fit in null-safe code.

~4 min readJava
#Java#Objects class#null handling#Java streams
Illustration of a Java null check comparing the Objects.isNull method with the equality operator.

Java's Objects.isNull() method tests whether a reference is null. It is most useful when you need a Predicate or method reference in stream code; for ordinary conditional checks, value == null is usually simpler. This article covers the syntax, the stream use case, and related Objects utilities.

Null checks are a routine part of Java code. The classic form is:

java
if (value == null) { // handle missing value }

Java 7 introduced the Objects utility class. Objects.isNull(Object obj) is a static method that returns true when the argument is null and false otherwise. It does not throw an exception and does not require any additional state.

The immediate question is whether Objects.isNull() should replace == null everywhere. The answer is more nuanced than a simple substitution, and the choice affects readability, stream pipelines, and how you structure validation logic.

Objects.isNull Syntax and Behavior

Objects.isNull is a static method that takes a single object reference and returns a boolean:

java
String name = null; boolean missing = Objects.isNull(name); // true

The implementation is effectively equivalent to obj == null. There is no special handling for primitives, collections, or arrays; the method simply tests the reference. Passing a non-null value returns false.

Because it is a static method, it can be written as the method reference Objects::isNull and passed to functional interfaces that expect a Predicate<T>. That is where it becomes more useful than the operator form.

Using Objects.isNull in Streams

The most common practical use of Objects.isNull is inside stream pipelines where a Predicate is required:

java
List<String> values = Arrays.asList("alpha", null, "gamma"); List<String> present = values.stream() .filter(Objects::nonNull) .toList();

The complementary Objects.nonNull filters out null references. You could write filter(v -> v != null), but the method reference reads more directly once you are familiar with the Objects API.

There is a subtle trap here. Using Objects::isNull as a filter predicate works:

java
List<String> missing = values.stream() .filter(Objects::isNull) .toList();

But using Objects::isNull inside map or flatMap rarely makes sense because those stages transform values rather than test them. The method returns a boolean, so mapping a stream of objects to a stream of booleans is usually a sign that the pipeline is structured incorrectly.

Objects.isNull vs the Equality Operator

For a single, imperative null check, value == null is shorter and more direct:

java
if (value == null) { throw new IllegalArgumentException("value is required"); }

Objects.isNull(value) is slightly more verbose and reads more formally than the operator form. In typical application code, performance is rarely a reason to choose between these forms; Objects.isNull is small enough that the JIT can inline it. The decision should rest on context.

Use == null when:

  • the check appears in ordinary conditional logic
  • the surrounding code already uses operators for other comparisons
  • the null check is incidental rather than the focus of the expression

Use Objects.isNull when:

  • you need a Predicate for a stream or a functional interface
  • you want to pass the check as a method reference
  • you are standardizing on the Objects API for null-related utilities

Objects.requireNonNull for Validation

Objects.isNull only tests a reference. When the goal is to fail fast on a missing argument, Objects.requireNonNull is the more appropriate tool:

java
public void process(String input) { Objects.requireNonNull(input, "input must not be null"); // safe to use input below }

This method throws NullPointerException with a custom message when the argument is null and returns the argument otherwise. That return value lets you reassign a null-checked reference in one line:

java
String safe = Objects.requireNonNull(input, "input must not be null");

Note the difference in intent. isNull answers a question; requireNonNull enforces a contract. Reaching for isNull inside a method body when you actually want to reject a null argument usually produces more code than necessary, because you still have to throw an exception manually.

Common Mistakes with Objects.isNull

A frequent mistake is using Objects.isNull where Optional handling is already in place. For example:

java
Optional<String> maybe = findValue(); if (Objects.isNull(maybe)) { // never true; Optional is never null here }

An Optional instance is itself an object, and the reference returned by a method is not null unless the method explicitly returns null. The check tests the Optional reference, not the value inside it. If you want to test whether the Optional contains a value, use maybe.isEmpty() or maybe.isPresent().

Another mistake is overusing Objects.isNull in ordinary code where the operator form is clearer. A chain of Objects.isNull calls can obscure the logic:

java
if (Objects.isNull(a) || Objects.isNull(b) || Objects.isNull(c)) { // handle missing values }

The operator version is shorter and equally clear:

java
if (a == null || b == null || c == null) { // handle missing values }

Readability and Maintainability Tradeoffs

Consistency matters more than choosing one style globally. If a codebase already uses the Objects utility class for requireNonNull and nonNull in streams, using isNull for symmetric checks keeps the style uniform. If the codebase relies on plain operators, introducing method-based checks only in isolated places creates inconsistency that readers have to reconcile.

The Objects methods also make null handling more discoverable for developers who are not yet familiar with the class. Reading Objects.isNull(x) in a stream pipeline signals intent more explicitly than a lambda that hides the same check.

For typical application code, the performance difference is negligible; the JIT can inline Objects.isNull. The real cost is readability and consistency, which is where the decision should be made.

Compatibility and Version Considerations

Objects.isNull and Objects.nonNull have been available since Java 7. Objects.requireNonNull with a message supplier was added in Java 8; the two-argument form with a plain message has existed since Java 7. These null-checking helpers do not themselves require newer language features, so they work in older production Java versions.

One version-related detail: Stream.toList() used in the examples above requires Java 16 or later. If the codebase targets Java 8 to 15, replace it with .collect(Collectors.toList()). The null-checking methods themselves carry no such constraint.

Try matching browser tools

All tools

Small utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Java guides

More in Java

All Java guides

The guides filed next to this one in Java, in library order.