Java language

Record component equality

Generated record equality compares record class and corresponding component values. Reference components use their equality semantics; arrays are not compared by contents automatically. If overriding equality, preserve the equals/hashCode contract.

Formal Syntax & Structure

java syntaxjava
boolean equals(Object other)

Parameters & Attributes

NameTypeDescription
otherObjectObject to compare.

Return Value

boolean

Whether the record values compare equal.

Working Example

Example Snippetjava
public class Main {    record Shelf(String aisle, int number) {}    public static void main(String[] args) {        Shelf a = new Shelf("B", 3);        Shelf b = new Shelf("B", 3);        System.out.println(a.aisle() + a.number());        System.out.println(a.equals(b));        System.out.println(a == b);        System.out.println(a.hashCode() == b.hashCode());    }}
Java Reference