J.II · Java 25 practice question
A developer compares a record instance against an instance of a different record type that happens to have the exact same component names and values. What does the implicitly declared equals(Object) method return?
record Size(int w, int h) {}
record Dim(int w, int h) {}
void main() {
IO.println(new Size(10, 20).equals(new Dim(10, 20)));
}
Check answer and explanation
Correct answer: false
Explanation
The program prints false because the record's equals(Object) method requires the argument to be an instance of the same record class R; otherwise it returns false.
Common trap
Records use nominal typing rather than structural typing, so matching component names and values across different record classes do not make them equal. The equals(Object) method checks the argument's type safely and returns false rather than throwing a ClassCastException.
Source: JLS SE 25 §8.10.3. Record Members
Checked against the cited text on 2026-11-25.