Introduction
Data classes are a special kind of class in Kotlin designed to hold data. By adding the data modifier, the compiler automatically generates useful boilerplate functions such as equals(), hashCode(), toString(), copy(), and componentN() functions used for destructuring declarations, saving developers from writing repetitive code by hand.
Cricket analogy: The data modifier is like a scorecard app auto-generating a player's full stat comparison, readable summary, an editable clone for a new match, and split fields for name/runs/wickets, instead of a scorer writing each by hand.
Syntax
data class User(val name: String, val age: Int)Explanation
A data class must declare at least one parameter in its primary constructor, and all primary constructor parameters typically become properties. The compiler-generated toString() returns a readable representation like User(name=Alice, age=30). equals()/hashCode() compare instances by their property values rather than reference identity. copy() creates a new instance with some properties changed while keeping others the same. componentN() functions enable destructuring, such as val (name, age) = user. Data classes are not well suited for mutable inheritance hierarchies since they cannot be open in a way that preserves generated function correctness, and inheriting from another open class is restricted.
Cricket analogy: A Player(name, runs) data class needs at least one constructor field like name; toString() reads like Player(name=Kohli, runs=82); equals() compares by stats not roster slot; copy() clones the player with runs updated after an inning; but data classes resist being subclassed into an 'AllRounder' hierarchy.
Example
data class User(val name: String, val age: Int)
fun main() {
val user1 = User("Alice", 30)
val user2 = user1.copy(age = 31)
val (name, age) = user2
println(user1)
println(user2)
println("Destructured: $name, $age")
println(user1 == User("Alice", 30))
}Output
User(name=Alice, age=30)
User(name=Alice, age=31)
Destructured: Alice, 31
trueKey Takeaways
- The data modifier auto-generates equals(), hashCode(), toString(), copy(), and componentN() functions.
- A data class must have at least one primary constructor parameter.
- equals() and hashCode() compare structural equality based on property values.
- copy() lets you create a modified shallow copy while reusing unchanged property values.
- componentN() functions enable destructuring declarations like val (a, b) = obj.
- Data classes are best for simple value holders, not complex mutable inheritance hierarchies.
Practice what you learned
1. Which functions does the Kotlin compiler auto-generate for a data class?
2. What is the minimum number of primary constructor parameters a data class must have?
3. What kind of equality does the generated equals() method implement?
4. What does the copy() function do?
Was this page helpful?
You May Also Like
Classes in Kotlin
Learn how Kotlin classes bundle state and behavior with concise constructor syntax and final-by-default semantics.
Constructors in Kotlin (Primary and Secondary)
Understand how primary and secondary constructors initialize Kotlin objects, including init blocks and constructor delegation.
Collections in Kotlin
An overview of how Kotlin groups objects into Lists, Sets, and Maps using its dual mutable/read-only collection model.
Sealed Classes in Kotlin
Sealed classes restrict a class hierarchy to a known, closed set of subclasses, enabling exhaustive when checks.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics