What is a Record Type?
Learn what a Java record type is, its immutability guarantees, and how it auto-generates equals, hashCode and toString.
Expected Interview Answer
A record type is an immutable, compiler-supported data carrier — introduced as `record` in Java — whose fields are declared once in the header and whose constructor, accessors, equals, hashCode and toString are all generated automatically.
Declaring `record Point(int x, int y) {}` gives you a final class with private final fields x and y, a canonical constructor, accessor methods x() and y(), and structurally correct equals/hashCode/toString, with no additional code required. Every field is implicitly final, so once constructed, a record’s state cannot change — this immutability is enforced by the language, not just a convention. Records can still declare additional methods, static members, and even a compact constructor for validating input during construction. They are specifically intended for simple, transparent carriers of data, not for objects with rich, mutable behavior or inheritance hierarchies, since records cannot extend another class.
- Zero-boilerplate immutable data carriers
- Compiler-enforced immutability prevents accidental mutation bugs
- Concise, self-documenting field declarations in the header
- Correct-by-construction equals/hashCode/toString
AI Mentor Explanation
An official man-of-the-match certificate is issued once, with the player’s name and figures printed on it permanently — nobody edits the certificate afterward, they issue a new one for a different match. A record type has this same guarantee: once constructed, its fields are permanently fixed, and the “certificate” (the record) is generated with the printed values, equality check and description all bundled in from the moment it’s created.
Step-by-Step Explanation
Step 1
Declare fields in the header
e.g. record Point(int x, int y) {} lists all state in one line.
Step 2
Compiler generates the canonical constructor
Assigns each header parameter to a private final field.
Step 3
Accessors and equality are generated
x(), y(), equals(), hashCode() and toString() are produced automatically.
Step 4
Add validation if needed
A compact constructor can validate arguments before the implicit field assignment.
What Interviewer Expects
- Correct mention of Java records specifically as a language feature
- Understanding that fields are implicitly final (immutable)
- Knowledge that records cannot extend another class
- Awareness of the compact constructor for validation
Common Mistakes
- Confusing “record” (Java 16+ language feature) with the general “data class” concept
- Believing records can be mutable
- Assuming records can extend another class (they implicitly extend Record and cannot extend further)
- Forgetting records can still implement interfaces and declare extra methods
Best Answer (HR Friendly)
“A record is a special, compiler-supported class for holding immutable data, where you declare the fields once and get the constructor, accessors, equals, hashCode and toString generated automatically. It’s designed specifically for simple, unchangeable data carriers rather than objects with complex behavior.”
Code Example
public record Range(int min, int max) {
// Compact constructor: validates before implicit field assignment
public Range {
if (min > max) {
throw new IllegalArgumentException("min cannot exceed max");
}
}
public int span() { return max - min; }
}
Range r = new Range(1, 10);
System.out.println(r.span()); // 9
System.out.println(r); // Range[min=1, max=10]Follow-up Questions
- Can a record implement an interface?
- Why can't a record extend another class?
- What is a compact constructor used for in a record?
- How is a record different from a general-purpose data class in another language?
MCQ Practice
1. Fields declared in a Java record are, by default?
Record header fields become private final fields, enforcing immutability at the language level.
2. A Java record can?
Records can implement interfaces, though they cannot extend another class since they implicitly extend Record.
3. A compact constructor in a record is primarily used for?
The compact constructor runs validation/normalization logic before the implicit field assignment occurs.
Flash Cards
Record type in one line? — An immutable, compiler-generated data carrier declared with fields in its header.
Introduced in which Java version? — Java 16 (as a standard feature, preview earlier).
Can records extend classes? — No — they implicitly extend Record and cannot extend further.
What validates constructor input? — A compact constructor, run before implicit field assignment.