What is a data class and what does it generate?
Assesses fundamental understanding of Kotlin conventions, runtime behavior, and memory/performance considerations.
Hiring managers look for precision, avoidance of ambiguous jargon, and ability to explain trade-offs under real production conditions.
A data class automatically generates equals(), hashCode(), toString(), copy() and componentN() functions for destructuring, based on the properties declared in its primary constructor.
data class User(val id: Int, val name: String)
val u = User(1, "Ada")
val renamed = u.copy(name = "Grace")
val (id, name) = renamed // destructuring
println(renamed) // User(id=1, name=Grace)
Requirements: the primary constructor must have at least one parameter, all parameters must be val or var, and the class cannot be abstract, open, sealed or inner. Only primary-constructor properties participate in the generated methods; properties declared in the body are ignored.
Use data classes for value-like holders, DTOs and results. For persistence entities, generated equals and hashCode based on a mutable id can cause subtle bugs in sets and maps, so some teams use regular classes there.
Candidate Response Strategy & Interview Tips
- Start with a concise one-sentence summary: Deliver a direct, confident answer first before expanding into nuances.
- Demonstrate real-world trade-offs: Discuss where this approach excels and when you would avoid it in production systems.
- Discuss complexity & edge cases: Proactively explain time/space complexity or boundary conditions (null values, scale limits).
- Prepare for interviewer follow-ups: Technical hiring panels frequently probe deeper into concurrency, backward compatibility, or alternative libraries.