java
programming
java.util.Objects
null-check
coding best practices

Java Objects.isNull vs == null

Codemia Knowledge Hub ·

Objects.isNull(obj) and obj == null do exactly the same check: isNull is implemented as return obj == null;. Use == null for ordinary if statements, and Objects.isNull when you need a method reference, such as filter(Objects::isNull) in a stream.

How Objects.isNull works

isNull is a static method on java.util.Objects, a utility class (added in Java 7, with isNull itself arriving in Java 8) that provides null-safe helpers and equality checks. Its signature:

java
public static boolean isNull(Object obj);

It returns true if the argument is null and false otherwise. The implementation is a single comparison:

java
public static boolean isNull(Object obj) {
    return obj == null;
}

Usage:

java
1String str = null;
2if (Objects.isNull(str)) {
3    System.out.println("String is null");
4} else {
5    System.out.println("String is not null");
6}

How == null works

The == operator is the traditional way to check whether a reference points to null. It is part of the language and compares the reference directly, with no method call:

java
1String str = null;
2if (str == null) {
3    System.out.println("String is null");
4} else {
5    System.out.println("String is not null");
6}

This is the form most Java developers use because it is short, universal and works in every Java version.

Side-by-side comparison

CharacteristicObjects.isNull(obj)obj == null
Available sinceJava 8Always in Java
Use caseMethod references, functional styleGeneral use
ReadabilityReads well in streams and other places that take a method referenceShort and direct
PerformanceDelegates to obj == null internallyAvoids a method call
Method referencesWorks as Objects::isNull, alongside Objects::nonNullCannot be used as a method reference

Where Objects.isNull is the better fit

The main reason to use isNull is that it can be passed as a method reference. Filtering a stream is the typical case:

java
  List<String> list = Arrays.asList("a", null, "b");
  list.stream().filter(Objects::isNull).forEach(System.out::println);

A method reference here is more concise than a lambda like s -> s == null, and the same applies to other functional interfaces: Objects::isNull and Objects::nonNull read better than nested conditionals inside lambda expressions.

If a codebase leans on streams, Optional and other functional APIs, using Objects.isNull and Objects.nonNull in those pipelines keeps the style consistent and easy to extend. For a plain if statement, it adds nothing over == null except a method call.

Performance and consistency

  • Performance: obj == null is evaluated directly, so it may be marginally faster by avoiding a method call. Otherwise the two are equivalent, since isNull wraps the same comparison.
  • Consistency: in team projects, pick one style for plain null checks and stick to it.
  • Older code: projects written before these helpers existed use == throughout, and there is no reason to convert them.

Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms