← Back to Home

== vs .equals()

Understanding the difference between == and .equals() is fundamental in Java and a very high-frequency interview question, especially for String, wrapper classes, and collections.

High-Level Difference (One Line)

  • == → compares references (memory addresses)
  • .equals() → compares content (logical equality)

== Operator

What It Does

  • Compares whether two references point to the same object
  • For primitive types, compares actual values

Example (Primitives)

int a = 10;
int b = 10;
System.out.println(a == b); // true
          

✔ Values compared

Example (Objects)

String s1 = new String("Java");
String s2 = new String("Java");
System.out.println(s1 == s2); // false
          

❌ Different memory locations

.equals() Method

What It Does

  • Compares object content
  • Defined in Object class
  • Many classes override it (String, wrappers, collections)

Default Implementation (Object class)

public boolean equals(Object obj) {
    return (this == obj);
}
          

✔ Same as == unless overridden

Example with String

String s1 = new String("Java");
String s2 = new String("Java");
System.out.println(s1.equals(s2)); // true
          
  • ✔ Content compared
  • ✔ String overrides equals()

String Pool Example (Interview Favorite)

String s1 = "Java";
String s2 = "Java";
System.out.println(s1 == s2);       // true (string pool)
System.out.println(s1.equals(s2));  // true
          

✔ Both references point to same pooled object

Wrapper Classes Example

Integer a = 100;
Integer b = 100;
System.out.println(a == b);        // true (cached)
System.out.println(a.equals(b));   // true

Integer x = 200;
Integer y = 200;
System.out.println(x == y);        // false
System.out.println(x.equals(y));   // true
          

✔ Integer cache range: -128 to 127

Custom Object Example (Important)

class Employee {
    int id;
    String name;
}

Employee e1 = new Employee(1, "A");
Employee e2 = new Employee(1, "A");
System.out.println(e1 == e2);        // false
System.out.println(e1.equals(e2));   // false
          
  • ❌ Content not compared
  • ✔ Need to override equals()

Overriding .equals() (Best Practice)

@Override
public boolean equals(Object obj) {
    if (this == obj) return true;
    if (obj == null || getClass() != obj.getClass()) return false;
    Employee e = (Employee) obj;
    return id == e.id && name.equals(e.name);
}
          

✔ Logical equality achieved

equals() Contract (Interview Must-Know)

  1. Reflexive: x.equals(x) → true
  2. Symmetric: x.equals(y) ⇔ y.equals(x)
  3. Transitive
  4. Consistent
  5. x.equals(null) → false

equals() and hashCode() Relationship (Preview)

  • If equals() is overridden
  • hashCode() must also be overridden
  • ✔ Required for HashMap, HashSet correctness

== vs .equals() (Interview Table)

Aspect == .equals()
Type Operator Method
Compares Reference Content
Primitives ✔ Value ❌ N/A
Objects Reference Content (if overridden)
Override possible ❌ No ✔ Yes

Common Beginner Mistakes

  • Using == to compare Strings
  • Forgetting to override equals() in custom classes
  • Ignoring Integer cache behavior
  • Not handling null in equals
  • Assuming .equals() always compares content

Interview-Ready Answers

Short Answer

== compares references, while .equals() compares object content.

Detailed Answer

In Java, the == operator checks whether two references point to the same memory location, whereas the .equals() method checks logical equality of objects. For classes like String, .equals() is overridden to compare content, while for custom classes it must be overridden explicitly.

Key Takeaway

Use == for primitives. Use .equals() for object content. This distinction is critical for Strings, collections, and custom domain objects.