Sub topic 14
Records: generated `equals`, `hashCode`, and `toString`
This chapter assumes you can write Java classes, use constructors, and understand object references. It also assumes basic knowledge of equals, hashCode, and toString.
A record is a compact Java class for carrying a fixed set of values. Java generates several methods from the record components:
- An accessor for each component.
- A constructor for all components.
equals.hashCode.toString.
Records were finalized in Java 16. Use a recent JDK when compiling them.
The generated methods
Consider this record:
public record Point(int x, int y) {
}The components are x and y. The record automatically provides x(), y(), equals, hashCode, and toString.
public class Main {
record Point(int x, int y) {
}
public static void main(String[] args) {
Point first = new Point(1, 2);
Point second = new Point(1, 2);
System.out.println(first.x()); // 1
System.out.println(first.equals(second)); // true
System.out.println(first.hashCode() == second.hashCode()); // true
System.out.println(first); // Point[x=1, y=2]
}
}The two points are different object instances, but they have equal component values. The generated equals method compares those values. The generated hashCode uses the same components, so equal records have equal hash codes. The generated toString includes the record name and component values.
The generated methods behave as if the record were a value object:
flowchart TD
A[record Point int x int y] --> B[Generated accessors]
A --> C[Generated equals]
A --> D[Generated hashCode]
A --> E[Generated toString]
C --> F[Compare component values]
D --> G[Hash the same components]
E --> H[Print record name and values]Equality uses the record's components
Two record objects are equal when they have the same record type and equal component values. A Point is not equal to a different record type that happens to contain x and y.
For reference components, generated equality calls the component object's equals method. For primitive components, it compares the primitive values.
| Component type | Generated behavior |
|---|---|
| `int`, `boolean`, and other primitives | Compares the values |
| `String` or another object | Uses that object's `equals` method |
| Array | Uses array reference equality, not element-by-element equality |
The array behavior is an important limitation. If a record contains an array, two arrays with the same contents are still different array objects. Their record instances will therefore usually not be equal.
public class Main {
record Scores(int[] values) {
}
public static void main(String[] args) {
Scores first = new Scores(new int[] {10, 20});
Scores second = new Scores(new int[] {10, 20});
System.out.println(first.equals(second)); // false
}
}A record is shallowly immutable. Its component fields cannot be reassigned after construction, but an object stored in a component may still be mutable. The array inside Scores can be changed after the record is created.
When to use records
Use a record when the object's identity is completely described by its components. Coordinates, measurements, configuration values, and small data-transfer objects are common examples.
You can add methods, validation, and constructors to a record. You can also override the generated methods, but do so only when the default component-based behavior is wrong for your design. If you override one of equals or hashCode, keep them consistent.
Exercise
What does new Point(1, 2).toString() produce for record Point(int x, int y) {}?
Show answer
It produces Point[x=1, y=2].