Recommended Free Tools
In Java, == compares primitive values but checks whether two object references point to the same object. For strings and other objects, equals() checks equality as the class defines it. For example, two separately created strings can have identical text while a == b is false and a.equals(b) is true.
What does == compare in Java?
The answer depends on the operands. With primitive values such as int, == compares the values. With object references, it tests identity: whether both references denote the same object. It does not inspect the object’s fields or compare its text.
As an Amazon Associate I earn from qualifying purchases.
The Java Language Specification states that, for reference operands, == is true when both values are null or both refer to the same object or array; otherwise it is false. The rule is documented in the Java SE 21 Language Specification and the Java SE 26 early-access specification. The latter is an early-access, version-sensitive page.
Do these 3 things before closing this tab:
1Scan for outdated or missing drivers - takes under a minute2Clear out junk files and repair common Windows errors3Fix the driver behind crashes, sound loss and screen glitchesWhy can two equal strings fail an == comparison?
Each call to new String(...) creates a distinct string object. The text can match without the references identifying the same object:
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
This code illustrates the documented behavior; it is not a report of a test run. The Java SE 21 String API specifies that String.equals(Object) returns true when the argument is a non-null String representing the same sequence of characters. The comparison is case-sensitive, so "Coffee".equals("coffee") is false.
Why do string literals sometimes make == true?
Java interns string literals and string-valued constant expressions. If two variables are initialized with the same literal, they may refer to the same pooled object, so == can be true. That result reflects shared identity, not a comparison of character contents.
Rank #2
A separately constructed string can contain the same characters but have a different identity, which is why the previous example can produce false for ==. The String API also documents intern(), which returns a canonical pooled representation: for strings s and t, s.intern() == t.intern() is true exactly when s.equals(t) is true. This explains interning; it is not a reason to use intern() as the ordinary way to compare strings.
Which comparison should you use?
| What you intend to compare | Use | Meaning |
|---|---|---|
Primitive values, such as two ints |
== |
Compares the values. |
| Whether two references identify 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 semantics defined by that class. |
| Two references that may be null | Explicit null handling or Objects.equals(a, b) |
A practical null-safe equality comparison. |
How should you compare strings when null is possible?
Calling equals() on a null reference throws a NullPointerException. By contrast, String.equals(null) returns false. If the receiver is guaranteed non-null, use a.equals(b). If either reference may be null, compare explicitly or use Objects.equals(a, b), which handles null values without calling a method on a null receiver.
What does equals() mean for other objects?
equals() is a method, so its meaning depends on the class. The default implementation inherited from Object uses identity equality. A class can override it to define value equality, such as equality based on selected fields. It does not have to compare every field; its behavior follows the class’s chosen semantics.
The Java SE 21 Object API documents the equals() contract. A valid implementation must be reflexive, symmetric, transitive, consistent while the information used in comparison remains unchanged, and return false when compared with null. There is also a related hashCode() rule: if two objects are equal according to equals(), they must have the same hash code.
Rank #4
When a custom object’s equality result surprises you, inspect that class’s equals() implementation or documentation. The method name alone does not tell you which fields or properties it considers.
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.




