Sub topic 2
== versus equals: identity and equality
Assume you can write and compile basic Java classes, distinguish primitive values from object references, and use List, Set, and Map. This chapter focuses on the difference between reference identity and logical equality.
Reference identity with `==`
The == operator answers a simple question for object references: do both references point to the same object?
String first = new String("hello");
String second = new String("hello");
String alias = first;
System.out.println(first == second); // false
System.out.println(first == alias); // truefirst and second contain the same characters, but new String creates two separate objects. They have different identities. alias refers to the exact object referenced by first, so the comparison is true.
For primitive types, == compares values directly:
int a = 10;
int b = 10;
System.out.println(a == b); // trueA reference comparison does not inspect the fields inside the referenced objects.
Logical equality with `equals`
equals is a method inherited from java.lang.Object. Its purpose is to let a class define what it means for two instances to be logically equal.
String overrides equals, so it compares character content:
String first = new String("hello");
String second = new String("hello");
System.out.println(first == second); // false
System.out.println(first.equals(second)); // trueThe default implementation in Object behaves like identity comparison. A class that does not override equals therefore considers two different instances unequal, even when their fields contain the same data.
The class decides which state matters. For a Point, equality might mean matching x and y values. For a User, it might mean matching a stable user ID.
Null and common mistakes
Calling a method on null throws NullPointerException:
String value = null;
System.out.println(value.equals("java")); // NullPointerExceptionWhen either value may be null, use Objects.equals:
import java.util.Objects;
String left = null;
String right = null;
System.out.println(Objects.equals(left, right)); // trueObjects.equals returns true when both references are null, false when only one is null, and otherwise calls left.equals(right).
Do not use == to compare strings or other value objects. It can appear to work with string literals because Java may reuse interned string objects, but that behavior does not make identity a content comparison.
| Expression | What it compares | Typical use |
|---|---|---|
| `a == b` | Primitive values or reference identity | Check whether two references are the same object |
| `a.equals(b)` | Class-defined logical equality | Compare object contents or identity as defined by the class |
| `Objects.equals(a, b)` | Logical equality with null handling | Compare possibly null values |
Exercise
What do these statements print, and why? String a = new String("x"); String b = new String("x"); System.out.println(a == b); System.out.println(a.equals(b));
Show answer
They print false and true. The variables reference different objects, but String.equals compares their character content.