100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Kotlin

Data Classes in Kotlin

See how Kotlin data classes auto-generate equals, hashCode, toString, copy, and componentN functions for value-holding objects.

Classes & ObjectsBeginner8 min readJul 8, 2026
Analogies

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

kotlin
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

kotlin
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

text
User(name=Alice, age=30)
User(name=Alice, age=31)
Destructured: Alice, 31
true

Key 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

Was this page helpful?

Topics covered

#Kotlin#KotlinProgrammingStudyNotes#Programming#DataClassesInKotlin#Data#Classes#Syntax#Explanation#OOP#StudyNotes#SkillVeris