Data classes are Kotlin's answer to Java records and C# records for immutable-ish carriers: the compiler generates equals, hashCode, toString, and copy when you mark a class with data.
Syntax and copy
data class Point(val x: Int, val y: Int)
val a = Point(1, 2)
val b = a.copy(y = 5)
Data classes must have at least one primary constructor parameter. They cannot be abstract, open, sealed, or inner.
Destructuring
val (x, y) = point uses component functions component1(), component2(), …—handy for tuples-like returns without a separate Pair type.
Important interview questions and answers
- Q: Data class vs regular class?
A: Data classes auto-generate structural equality and copy; use them for DTOs and value-like aggregates, not deep inheritance trees. - Q: Are data class properties always immutable?
A: No—varin the header is allowed, butvalis idiomatic for predictable equality.
Self-check
- What method creates a modified duplicate?
- Can a data class be open?
Tip: Use copy() for immutable-style updates instead of mutating shared DTOs in concurrent code.
Interview prep
- What is generated?
equals, hashCode, toString, copy, componentN.
- When to use?
DTOs and value-like aggregates—not deep inheritance.