Why Objects.equals Beats a.equals in Java: Null Safety and Source Code Explained
This article explains how Java's Objects.equals differs from the traditional a.equals(b) method, covering null and empty‑string scenarios, demonstrating safe comparisons without NullPointerExceptions, and includes a concise analysis of the underlying source code and the distinction between a==b and a.equals(b).
1. Null values
1. a.equals(b) when a is null throws NullPointerException.
2. a.equals(b) when a is not null and b is null returns false.
3. Objects.equals(a, b) returns true if both are null, false if exactly one is null, and never throws NullPointerException.
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. Empty string values
If both a and b are empty strings (""), a.equals(b) returns true; otherwise it returns false. Objects.equals behaves the same in these cases.
"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
1. 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.
* Consequently, if both arguments are null, true is returned
* and if exactly one argument is null, false is returned.
* Otherwise, equality is determined by using the Object.equals method
* of the first argument.
*/
public static boolean equals(Object a, Object b) {
return (a == b) || (a != null && a.equals(b));
}
}2. Explanation
The method first checks reference equality ( a == b); if true, it returns true without further checks. 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)
a==bcompares object references and returns true only when both references point to the same object in memory. a.equals(b) performs logical comparison based on the object's content; it returns true when the objects are considered equal, and typically requires overriding to provide meaningful equality logic.
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 High-Performance Architecture
Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.
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.
