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
It covers all three cases correctly:
- both values are
null, - one is
nulland 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:
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:
Typical output:
- Use
==only when you care whether both references point to the same object. - Use
equalsorObjects.equalswhen 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:
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:
This is not an equality check, and it needs a different tool.
Mistakes to avoid
- Calling
a.equals(b)whenamay benull. - Using
==for object equality when value equality is intended. - Forgetting to implement
equalsandhashCodeproperly in custom classes. - Confusing equality comparison with ordering or sorting.
- Writing manual null-check ladders where
Objects.equalsalready expresses the intent.
Related reading
- Comparing arrays in JUnit assertions, concise built-in way?
- Comparing strings with which are declared final in Java
- Comparing two java.util.Dates to see if they are in the same day
- Compilation error after upgrading to JDK 21 - NoSuchFieldError JCImport does not have member field JCTree qualid
- Compile error Class file has wrong version 52.0, should be 50.0
- CompletableFuture in loop How to collect all responses and handle errors
- CompletableFuture not working as expected
- CompletableFuture thenApply vs thenCompose
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData 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.