Java
Programming
Coding Differences
Comparison Operators
Object Comparison

What is the difference between == and equals() in Java?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In Java, == and equals() answer different comparison questions. == checks whether two primitives have the same value or whether two references point to the same object. equals() is a method intended for logical equality, which means classes can override it to compare object contents instead of object identity.

== with Primitive Values

For primitives, == compares actual values.

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

This is straightforward because primitives are values, not objects.

== with Object References

For objects, == compares references, not contents.

java
1String s1 = new String("hello");
2String s2 = new String("hello");
3
4System.out.println(s1 == s2); // false

These two references point to different objects in memory, so == is false even though the text content matches.

equals() Is for Logical Equality

equals() is designed for content-based comparison when a class overrides it meaningfully.

java
1String s1 = new String("hello");
2String s2 = new String("hello");
3
4System.out.println(s1.equals(s2)); // true

String overrides equals() to compare character content, not identity.

Default equals() Still Uses Identity

The default implementation from Object behaves much like == for references. That means if you create your own class and do not override equals(), then equals() and == effectively tell you the same thing.

java
1class Person {
2    String name;
3
4    Person(String name) {
5        this.name = name;
6    }
7}
8
9Person p1 = new Person("Ada");
10Person p2 = new Person("Ada");
11
12System.out.println(p1 == p2);       // false
13System.out.println(p1.equals(p2));  // false

Override equals() for Value Semantics

If two objects should be considered equal based on their fields, override equals().

java
1import java.util.Objects;
2
3class Person {
4    private final String name;
5
6    Person(String name) {
7        this.name = name;
8    }
9
10    @Override
11    public boolean equals(Object obj) {
12        if (this == obj) return true;
13        if (!(obj instanceof Person other)) return false;
14        return Objects.equals(this.name, other.name);
15    }
16
17    @Override
18    public int hashCode() {
19        return Objects.hash(name);
20    }
21}

Now two Person objects with the same name can be logically equal.

equals() and hashCode() Must Match

If you override equals(), you should also override hashCode(). Otherwise, hash-based collections such as HashSet and HashMap behave inconsistently.

This is one of the most important rules in Java object equality.

Use Objects.equals() for Null Safety

Calling a.equals(b) can throw a NullPointerException if a is null. A safer helper is Objects.equals(a, b).

java
1import java.util.Objects;
2
3String a = null;
4String b = "hello";
5
6System.out.println(Objects.equals(a, b)); // false

That is often the best choice in application code when nulls are possible.

Strings Cause a Lot of Confusion

Strings are where many Java developers first notice the difference. Sometimes == appears to work because string literals may be interned, but that is not the same as content comparison. If you want to know whether two strings contain the same characters, use equals() or Objects.equals(), not ==.

Common Pitfalls

  • Using == for object content comparison when the real need is logical equality.
  • Forgetting that the default equals() from Object still compares identity.
  • Overriding equals() without also overriding hashCode().
  • Calling .equals() on a possibly null reference instead of using a null-safe comparison helper.
  • Getting confused by string interning cases where == sometimes appears to work for strings by accident.

Summary

  • '== compares primitive values or object identity.'
  • 'equals() compares logical equality when a class overrides it meaningfully.'
  • For many built-in value-like classes such as String, equals() checks content.
  • If you override equals(), you should also override hashCode().
  • Use Objects.equals() when null safety matters.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.