Why Objects.equals Handles Nulls Safely: A Deep Dive into Java Equality
This article explains how Objects.equals differs from a.equals when handling nulls and empty strings, analyzes the underlying source code, and clarifies the distinction between reference (a==b) and logical (a.equals(b)) comparisons in Java.
I originally thought this method was introduced in Java 8, but it actually dates back to Java 1.7, so I examined the source code.
1. When the value is null
1. a.equals(b) throws a NullPointerException if a is null. 2. a.equals(b) returns false when a is non‑null and b is null. 3. Objects.equals(a, b) returns true if both are null, false if exactly one is null, and never throws an exception.
null.equals("abc") → throws NullPointerException
"abc".equals(null) → returns false
null.equals(null) → throws NullPointerException Objects.equals(null, "abc") → returns false
Objects.equals("abc", null) → returns false
Objects.equals(null, null) → returns true2. When the value is an empty string
If both a and b are empty strings (""), a.equals(b) returns true; otherwise it returns false. In this case Objects.equals behaves the same.
"abc".equals("") → returns false
"".equals("abc") → returns false
"".equals("") → returns true Objects.equals("abc", "") → returns false
Objects.equals("", "abc") → returns false
Objects.equals("", "") → returns true3. Source code analysis
Source
public final class Objects {
private Objects() {
throw new AssertionError("No java.util.Objects instances for you!");
}
/**
* Returns true if the arguments are equal to each other and false otherwise.
* If both arguments are null, true is returned; if exactly one is null, false is returned.
* Otherwise equality is determined by calling a.equals(b).
*/
public static boolean equals(Object a, Object b) {
return (a == b) || (a != null && a.equals(b));
}
}Explanation
The method first checks reference equality ( a == b); if that is true it returns true immediately. If not, it ensures a is not null before invoking a.equals(b), which prevents a NullPointerException.
4. Difference between a==b and a.equals(b)
When a and b are objects, a==b compares their references and returns true only if they point to the same heap object. a.equals(b) performs logical comparison of content; for custom classes it should be overridden to define logical equality.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
