☕ Java Advanced

What are Java records?

Why Interviewers Ask This

This is a differentiating question used for senior and lead roles. Interviewers want to see if you can explain not just what happens, but why — and what the trade-offs are in different approaches.

Answer

Records (Java 16, finalized from preview in 14) are a special class declaration designed for transparent, immutable data carriers. record Point(int x, int y) { } automatically generates: a canonical constructor, private final fields, public getters (x() and y()), and correct equals(), hashCode(), and toString() implementations. Records cannot extend other classes (they implicitly extend Record), cannot have non-static instance fields beyond the record components, and are implicitly final. You can add compact constructors for validation, custom methods, and static fields. Records are the Java equivalent of Kotlin data classes and are ideal for DTOs, value objects, and immutable data.

Pro Tip

Back up your answer with a specific project or situation. Saying 'In my last Java project, I used this when...' immediately makes your answer more credible and memorable.