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.
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.
This is straightforward because primitives are values, not objects.
== with Object References
For objects, == compares references, not contents.
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.
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.
Override equals() for Value Semantics
If two objects should be considered equal based on their fields, override equals().
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).
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()fromObjectstill compares identity. - Overriding
equals()without also overridinghashCode(). - 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 overridehashCode(). - Use
Objects.equals()when null safety matters.
Related reading
- What is the difference between ? and Object in Java generics?
- What is the difference between a HashMap and a TreeMap?
- What is the difference between a JavaBean and a POJO?
- What is the difference between a static and a non-static initialization code block
- What is the difference between an int and an Integer in Java and C#?
- What is the difference between and in Java?
- What is the difference between ArrayList.clear() and ArrayList.removeAll()?
- What is the difference between atomic / volatile / synchronized?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack 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.