CSE 331�Software Design & Implementation
Autumn 2026
Section 6 - Equals
Administrivia
Midterm this Friday 11/13 during lecture !!!!
- Mock exam (today): Tuesday 11/10, 4:30 - 6:20 PM
- Midterm review session: Thursday 11/12, 4:30 - 6:20 PM
- Both in CSE2 G01
Equals and hashCode
The equals method (review)
The contract that every override of Object.equals must satisfy:
Reflexive:
an object equals itself.
x.equals(x) == true
Symmetric:
reversing the operands preserves the result.
x.equals(y) == y.equals(x)
Transitive:
equality carries through a chain.
x.equals(y) && y.equals(z) implies x.equals(z)
Consistent:
repeated calls return same result, unless equality-relevant state changes
x.equals(y) // same result until mutation
Null uniqueness:
non-null objects must not be equal to null
x.equals(null) == false
Additional notions of equality
Reference equality:
references point to same object in memory
String a = new String(“hi”);
String b = a;
a == b; // true
Behavioral equivalence:
no sequence of ordinary operations can distinguish the objects
String a = new String(“hi”);
String b = new String(“hi”);
a.equals(b); // true
Observational equivalence:
cannot distinguish the objects without mutating them or testing identity
Date a = new Date(0);
Date b = new Date(0);
a.getTime() == b.getTime(); // true
The hashCode method (review)
The contract that every override of Object.hashCode must satisfy:
Self-consistent:
repeated calls return the same result unless equality-relevant state changes
x.hashCode() // same result until mutation
Equality-consistent:
equal objects must have equal hash codes
x.equals(y) implies x.hashCode() == y.hashCode()
Important:
Method Overriding
Overriding equals
Overriding:
public bool equals(Object o) { ... }
public bool equals(Object d) { ... }
Overloading (not an override):
public bool equals(Whale o) { ... }
Task 1: Thing 1 and Thing 2
Consider the following class Thing, which, unfortunately, doesn't properly override equals, but overloads it instead.
class Thing {
private int contents;
public Thing(int value) {
this.contents = value;
}
public boolean equals(Thing other) {
return this.contents == other.contents;
}
}
Task 1: Thing 1 and Thing 2
For each method call, indicate which equals method runs, and whether it prints true or false.
public static void main(String[] args) {
Thing t = new Thing(17);
Object o = t;
Thing u = new Thing(17);
// Method called Output
System.out.println(t.equals(o));
System.out.println(t.equals(u));
System.out.println(o.equals(u));
System.out.println(u.equals(o));
System.out.println(u.equals((Thing) o));
}
Task 2: Wrecked Angle
Here is a small class with a correctly overridden equals method.
public class ColoredRectangle {
private int width;
private int height;
private String color;
/** Equality for ColoredRectangles. Two ColoredRectangles
* are the same if they have the same width and height.
*/
@Override
public boolean equals(Object o) {
if (! (o instanceof ColoredRectangle)) {
return false;
}
ColoredRectangle cr = (ColoredRectangle) o;
return this.width == cr.width && this.height == cr.height;
}
}
Task 2: Wrecked Angle
For each implementation: Does it satisfy the hashCode contract? If yes, identify one type of collision between unequal rectangles. If no, explain why.
(Recall: ^ is bitwise XOR.)
return width;
return color.hashCode();
return 42;
return width ^ height ^ color.hashCode();
return width ^ height;
Typical hashCode implementations
@Override�public int hashCode() {� int result = Integer.hashCode(size);� result = 31 * result + Integer.hashCode(weight);� return result;�}
@Override�public int hashCode() {� return Objects.hash(size, weight);�}
Two example implementations:
Correct and effective hash functions minimize collisions for expected inputs.
Exam Practice
Exam-taking strategies
Time to vote
End
Please turn in your
half-sheets!