Recommended Free Tools
iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
For object references, == asks whether both variables refer to the same object; equals() asks whether the objects count as equal according to their class. For strings, that usually means comparing their character sequences. Two strings can therefore look identical and still produce false with ==.
What does == compare in Java?
It depends on the operands. With primitive values such as int, == compares the values. With object references, it compares identity: whether both references denote the same object. The Java Language Specification states that, for reference operands, == is true when both are null or both refer to the same object or array; otherwise, it is false. It does not compare an object’s fields or textual contents. See the Java SE 21 Language Specification.
For example, these two variables refer to distinct objects even though the strings contain the same characters:
Free tools Windows power users keep installed
One-click scans. No signup required.
String a = new String("coffee");
String b = new String("coffee");
System.out.println(a == b); // false: distinct String objects
System.out.println(a.equals(b)); // true: same character sequence
The first expression asks whether a and b refer to one object. The second asks whether the strings have the same sequence of characters.
Why can == appear to work with string literals?
Java interns string literals and string-valued constant expressions. As a result, variables initialized with the same literal may refer to the same pooled object, so == can be true. That result reflects shared identity, not a comparison of the characters.
A separately constructed string can have exactly the same contents without sharing that identity. The specification explicitly notes that distinct String objects compare false with ==, even when their character sequences match. The Java SE 21 String API documentation describes intern() as returning a canonical representation; it also states that s.intern() == t.intern() is true exactly when s.equals(t) is true.
Rank #2
Do not use intern() as a general fix for string comparisons. Use equals() to compare contents. Interning changes how strings are shared; it does not make identity comparison the right way to ask whether two strings have equal contents.
Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchWindows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallWhich comparison should you use?
| What you want to compare | Use | Meaning |
|---|---|---|
Primitive values, such as two ints |
== |
Compares the primitive values. |
| Whether two references denote the same object | == |
Compares object identity. |
| String contents | String.equals() |
Checks for the same character sequence, case-sensitively. |
| Custom-object equality | The class’s equals() contract |
Uses the equality meaning defined by that class. |
| Equality where either reference might be null | Explicit null handling or Objects.equals(a, b) |
Provides a null-safe comparison. |
How do you compare strings when null is possible?
Calling equals() on a null reference throws a NullPointerException. By contrast, a non-null string’s equals(null) call returns false, as specified by the String API.
If a is guaranteed to be non-null, a.equals(b) is the direct content comparison. If either reference might be null, explicitly handle null or use Objects.equals(a, b), which treats two null references as equal and compares non-null values through their equality methods.
What does equals() mean for other objects?
equals() is a method, so its behavior depends on the class. The default implementation inherited from Object uses identity equality. A class can override it to define value equality—for example, equality based on selected fields. It does not have to compare every field; the class defines which characteristics determine equality.
Rank #4
When a class overrides equals(), its implementation should follow the contract documented by the Java SE 21 Object API:
- Reflexive: an object equals itself.
- Symmetric: if
x.equals(y)is true, theny.equals(x)is true. - Transitive: if
x.equals(y)andy.equals(z)are true, thenx.equals(z)is true. - Consistent: repeated comparisons return the same result while the information used in the comparison has not changed.
- False for a comparison with
null.
There is also a required relationship with hashCode(): if two objects are equal according to equals(), they must have the same hash code. This matters when objects are used in hash-based collections.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

