Java Records: Immutable Data Carriers
Fast recall
Quick revision
Core answer
Whether you understand the purpose of records, what they auto-generate, their limitations (no inheritance, fields are final), and when they are appropriate vs when a plain class is better
Thinking records are just shorthand for Lombok's @Data — they are more constrained (immutable, no subclassing) and semantically different (their identity is entirely their data)
Mention compact constructors for validation, record patterns in Java 21 switch expressions, and the comparison with sealed classes for modelling ADTs
Interview-ready guide
Interview answer
Records are immutable data carriers — all fields are final, the class itself is final, and the compiler auto-generates a canonical constructor, accessors, equals(), hashCode(), and toString() based on all components.
What the compiler generates:
private finalfields for each component- Accessor methods named
x()andy()— notgetX(), breaking JavaBean convention equals()/hashCode()covering all fields — safe as HashMap keys out of the box
Compact constructors:
- Validate without boilerplate:
record Email(String value) { Email { if (!value.contains("@")) throw ...; value = value.toLowerCase(); } } - Modify the parameter, not the field —
this.value = valuehappens automatically after the body
Limitations and gotchas:
- Cannot extend classes (implicitly extends
java.lang.Record) or be subclassed - Mutable components (like
List) are not defensively copied — useList.copyOf(items)in the compact constructor - Accessors use
x()notgetX()— some JSON serializers and older frameworks need configuration
I recommend records for DTOs, composite return types, and map keys — for JPA entities or anything needing mutable state, use regular classes.
Java Records: Immutable Data Carriers
Direct Answer
A record (Java 16+) is a special class declaration for transparent, immutable data carriers. The compiler auto-generates a canonical constructor, private final fields, equals(), hashCode(), and toString() based on the components you declare. Use records for pure value objects: DTOs, domain events, query results, configuration tuples.
Deep Dive & Architecture
Varargs (`Type...
args`) = syntactic sugar — compiler converts to array.
A record (Java 16+) is a special class declaration for transparent, immutable data carriers. The compiler auto-generates a canonical constructor, private final fields, equals(), hashCode(), and toString() based on the components you declare. Use records for pure value objects: DTOs, domain events, query results, configuration tuples.
Saying Java is pass-by-reference for objects because you can modify the object's fields inside a method
Common Mistakes & Pitfalls
Thinking records are just shorthand for Lombok's @Data — they are more constrained (immutable, no subclassing) and semantically different (their identity is entirely their data)
Check your understanding
Answer these without looking back at the article.
- Is Java pass-by-value or pass-by-reference?
- Varargs in Java — Variable Arguments and Heap Pollution
- What is autoboxing and unboxing in Java? What is the Integer cache trap?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.