Records
A record is a compact class for immutable data. You list the components; the compiler supplies the fields, constructor, accessors, equals, hashCode, and toString.
Detailed Explanation
Most classes you write for "a pair of values" are boilerplate: private final fields, a constructor that assigns them, getters, and value-based equals/hashCode. A record is that class with the boilerplate generated from the header. record Point(int x, int y) {} is a full type.
Records arrived as a standard feature in Java 16. They are implicitly final. You cannot extend a record, and a record cannot extend anything but Object. They can implement interfaces. Components are private and final. Accessors use the component name (x(), not getX()). The generated equals and hashCode compare every component. toString prints the type name and values.
You can still write members. A compact constructor (Point { if (x < 0) throw ...; }) runs validation without repeating the assignment list; the compiler assigns components afterward. You can add methods, static factories, and nested types. You cannot add instance fields that are not components. The state is the header, always.
You reach for a record when the type is a transparent data carrier: a DTO, a map key, a method result with two fields, a pattern-matching target. You avoid a record when the type has an identity (an entity with a lifecycle), when it must extend another class, or when you need mutability. A mutable "record" is the wrong tool; use a class.
Records work well with pattern matching. if (obj instanceof Point(int x, int y)) binds the components. Sealed interfaces plus records give you algebraic data types: a Shape that is a Circle or a Rectangle, each a record, and a switch that the compiler checks for exhaustiveness.
The cost is the transparency. Callers will depend on the component list and the accessor names. Reordering components changes the canonical constructor and toString. Replacing a record with a class later is possible but noisy. Treat the header as published API. Nested records that hold mutable arrays still leak mutation if you return the array; copies belong in the compact constructor and accessors.
Code Examples
public record Money(String currency, long minor) {
public Money {
// Compact constructor: validate, then the compiler assigns fields.
if (currency == null || currency.length() != 3) {
throw new IllegalArgumentException("ISO currency required");
}
if (minor < 0) {
throw new IllegalArgumentException("minor must be >= 0");
}
}
public Money plus(Money other) {
if (!currency.equals(other.currency)) {
throw new IllegalArgumentException("currency mismatch");
}
return new Money(currency, minor + other.minor);
}
public static void main(String[] args) {
Money a = new Money("INR", 250);
Money b = a.plus(new Money("INR", 50));
System.out.println(b); // prints: Money[currency=INR, minor=300]
System.out.println(a.equals(new Money("INR", 250))); // prints: true
}
}sealed interface Shape permits Circle, Rect {}
record Circle(int r) implements Shape {}
record Rect(int w, int h) implements Shape {}
public class Areas {
static int area(Shape s) {
return switch (s) {
case Circle c -> c.r() * c.r() * 3; // rough, keeps the example integer
case Rect r -> r.w() * r.h();
};
}
public static void main(String[] args) {
System.out.println(area(new Rect(3, 4))); // prints: 12
}
}Common Gotchas
Arrays and mutable components break value equality
two records with equal-looking arrays are not equal unless you copy and compare contents yourself.
No extra instance fields
derived values must be methods, not fields. Cache only with care (and usually do not).
Accessors are `x()`, not `getX()`
frameworks that assume JavaBeans getters need a bridge method or a different mapping.
The canonical constructor is public API
changing component order breaks call sites and serialization shapes.