Java records vs a classic data class: 1 line vs 34
How many lines does a Java record save over a classic data class?
A three-field data holder with constructor, accessors, equals, hashCode and toString took 34 lines as a classic class and 1 line as a record (Java 16+), with identical toString, equals and hashCode behavior in a test run.
Results
| Classic class | 34 lines |
|---|---|
| Record | 1 line |
How it was measured
PersonClass (name, age, email) written by hand versus the equivalent record; both compiled and compared.
public record PersonRecord(String name, int age, String email) {}Why
The compiler generates the canonical constructor, accessors, equals, hashCode and toString for records.
When this does not apply
Records are immutable. They are not a fit for mutable JPA entities or classes that need inheritance.
Environment
MacBook Air (Apple silicon, 8 cores), macOS, JDK 17. Measured 2026-10-12.
Source: shorts/demo-ticket/java/PersonClass.java in my content-engine repository. Treat any single run as a direction; re-run it on your hardware.
FAQ
- Which Java version added records?
- Records became a standard feature in Java 16.
By Josué Barros, senior software engineer (full-stack, data and AI).