Fundamentals 6 min read

Understanding the Differences Between a.equals(b) and Objects.equals(a, b) in Java

This article explains how a.equals(b) behaves when a or b is null or an empty string, compares it with Objects.equals, and analyzes the underlying source code to clarify the differences between reference equality (a==b) and logical equality (a.equals(b)) in Java.

Java Captain
Java Captain
Java Captain
Understanding the Differences Between a.equals(b) and Objects.equals(a, b) in Java

I always thought this method was introduced in Java 8, but discovered it actually dates back to Java 1.7, so I examined the source code. This article summarizes the differences between a.equals(b) and Objects.equals(a, b) and provides a brief source‑code analysis.

1. When the value is null

1. a.equals(b) – if a is null , a NullPointerException is thrown.

2. a.equals(b) – if a is not null but b is null , the method returns false .

3. Objects.equals(a, b) – returns true when both a and b are null ; returns false when exactly one of them is null . No NullPointerException is thrown.

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 true

2. When the value is an empty string

1. If both a and b are empty strings ( "" ), a.equals(b) returns true ; otherwise it returns false .

2. In this situation Objects.equals behaves identically to a.equals(b) .

"abc".equals("") → returns false
"".equals("abc") → returns false
"".equals("")    → returns true
Objects.equals("abc", "") → returns false
Objects.equals("", "abc") → returns false
Objects.equals("", "")    → returns true

3. 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.
     * If both arguments are null, true is returned; 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 whether the two references are identical ( a == b ). If they are, it returns true immediately.

If the references differ, the expression (a != null && a.equals(b)) is evaluated. It ensures that a is not null before invoking a.equals(b) , thereby avoiding a NullPointerException .

Consequently, when both arguments are null the first condition ( a == b ) is true. When they are different non‑null objects, the method relies on the logical equality defined by a.equals(b) .

4. Difference between a == b and a.equals(b)

If a and b are objects, a == b compares their references; it returns true only when both references point to the exact same object in memory.

In contrast, a.equals(b) performs a logical comparison of the objects' contents. It returns true when the objects are considered equal according to the class's overridden equals method, which may involve comparing fields rather than references.

PS: If you found this share useful, feel free to give it a like or a view.

JavanullpointerexceptionReference EqualityObjects.equalsLogical Equality
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.