Sub topic 3
The equals contract
This chapter assumes you can write basic Java classes, use constructors, and work with object references. You should also know that a class can override an inherited method.
The equals method defines logical equality: when two separate objects should count as representing the same value. Every class inherits equals(Object) from java.lang.Object. The default implementation compares references, which is usually the same result as ==. Value classes often override it to compare selected fields instead.
The Java equals contract has five rules. A correct implementation must follow all of them.
The five rules
- Reflexive: For a non-null object
x,x.equals(x)must betrue. - Symmetric: For non-null objects
xandy,x.equals(y)must have the same result asy.equals(x). - Transitive: For non-null objects
x,y, andz, ifx.equals(y)andy.equals(z)are true, thenx.equals(z)must also be true. - Consistent: Repeated calls must return the same result while the fields used for equality do not change.
- False for null: For every non-null object
x,x.equals(null)must befalse.
These rules describe behavior, not a particular implementation. A class can use different fields or comparison techniques as long as the behavior remains valid.
public final class Coordinate {
private final int x;
private final int y;
public Coordinate(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof Coordinate)) {
return false;
}
Coordinate coordinate = (Coordinate) other;
return x == coordinate.x && y == coordinate.y;
}
public static void main(String[] args) {
Coordinate a = new Coordinate(3, 4);
Coordinate b = new Coordinate(3, 4);
Coordinate c = new Coordinate(3, 4);
System.out.println(a.equals(a)); // true: reflexive
System.out.println(a.equals(b)); // true
System.out.println(b.equals(a)); // true: symmetric
System.out.println(a.equals(b)
&& b.equals(c)
&& a.equals(c)); // true: transitive
System.out.println(a.equals(null)); // false
}
}The parameter type must be exactly Object. A method such as equals(Coordinate other) is an overload, not an override. Collections call equals(Object), so they will not use the overloaded method.
Choosing what equality means
Before writing equals, decide which fields define the value. Two coordinates with the same x and y are equal, so both fields participate. A display label or cached calculation might not participate.
Every comparison must work in both directions. If a base class considers a subclass equal but the subclass adds a field that the base class ignores, symmetry can break. This is one reason immutable value classes are often declared final.
The contract connects to hash-based collections. If two objects are equal, their hashCode values must also be equal. Otherwise, a HashSet or HashMap may fail to find an equal object. The reverse is not required: unequal objects may share a hash code.
Exercise
Which contract rule is broken if a.equals(b) returns true but b.equals(a) returns false?
Show answer
Symmetry is broken.