Java
Null Values
Object Comparison
Programming
Coding Techniques

Null-safe equals in Java: compare two objects that may be null

Codemia Knowledge Hub ·

Use Objects.equals(a, b). It is the standard null-safe way to compare two Java objects: it never throws NullPointerException, and it states clearly that you mean value equality.

How Objects.equals handles null

java
1import java.util.Objects;
2
3public class Main {
4    public static void main(String[] args) {
5        String a = null;
6        String b = null;
7        String c = "hello";
8
9        System.out.println(Objects.equals(a, b));
10        System.out.println(Objects.equals(a, c));
11        System.out.println(Objects.equals(c, "hello"));
12    }
13}

It covers all three cases correctly:

  • both values are null,
  • one is null and the other is not,
  • both are non-null and should be compared with equals.

That makes it the right default for ordinary equality checks.

Why a.equals(b) is risky

a.equals(b) is only safe if you already know a is not null:

java
1String a = null;
2String b = "hello";
3
4// a.equals(b); // throws NullPointerException

That is why older Java code is full of manual null checks. Objects.equals exists to remove that boilerplate. A single Objects.equals(a, b) call reads as "null-safe equality" immediately, which is easier to trust in review than a nested conditional or a clever one-liner.

== versus equals

For objects, == compares references, not values. For most domain objects that is not what you want:

java
1String x = new String("hello");
2String y = new String("hello");
3
4System.out.println(x == y);
5System.out.println(x.equals(y));

Typical output:

text
false
true
  • Use == only when you care whether both references point to the same object.
  • Use equals or Objects.equals when you care about value equality.

Implementing equals in your own classes

A null-safe wrapper only helps if the class implements equals (and hashCode) consistently:

java
1import java.util.Objects;
2
3class Person {
4    private final Integer id;
5    private final String name;
6
7    Person(Integer id, String name) {
8        this.id = id;
9        this.name = name;
10    }
11
12    @Override
13    public boolean equals(Object obj) {
14        if (this == obj) return true;
15        if (!(obj instanceof Person other)) return false;
16        return Objects.equals(id, other.id) && Objects.equals(name, other.name);
17    }
18
19    @Override
20    public int hashCode() {
21        return Objects.hash(id, name);
22    }
23}

With equals implemented like this, comparing two Person objects with Objects.equals is straightforward.

Null-safe ordering is a different problem

Sometimes "compare" really means sort or order. For ordering with possible null values, use comparators such as Comparator.nullsFirst or Comparator.nullsLast:

java
1import java.util.Arrays;
2import java.util.Comparator;
3import java.util.List;
4
5List<String> values = Arrays.asList("beta", null, "alpha");
6values.stream()
7      .sorted(Comparator.nullsLast(String::compareTo))
8      .forEach(System.out::println);

This is not an equality check, and it needs a different tool.

Mistakes to avoid

  • Calling a.equals(b) when a may be null.
  • Using == for object equality when value equality is intended.
  • Forgetting to implement equals and hashCode properly in custom classes.
  • Confusing equality comparison with ordering or sorting.
  • Writing manual null-check ladders where Objects.equals already expresses the intent.

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