Kotlin Data Classes Cheat Sheet
Covers declaring data classes, the auto-generated equals/hashCode/toString/copy methods, destructuring, and best practices for immutable models in Kotlin.
1 PageBeginnerMar 25, 2026
Declaring Data Classes
A one-line declaration generates equals, hashCode, and toString.
kotlin
data class User(val id: Int, val name: String, val email: String)val user1 = User(1, "Ana", "ana@example.com")val user2 = User(1, "Ana", "ana@example.com")println(user1 == user2) // true (structural equality via equals())println(user1) // User(id=1, name=Ana, email=ana@example.com)println(user1.hashCode() == user2.hashCode()) // true
The copy() Function
Creating a modified copy while keeping the original immutable.
kotlin
data class User(val id: Int, val name: String, val email: String)val user = User(1, "Ana", "ana@example.com")// copy() creates a new instance, overriding only the named parametersval renamed = user.copy(name = "Ana Maria")println(renamed) // User(id=1, name=Ana Maria, email=ana@example.com)println(user) // original is unchanged (immutability preserved with val)
Destructuring Declarations
Unpacking a data class into individual variables.
kotlin
data class Point(val x: Int, val y: Int)val point = Point(10, 20)val (x, y) = point // uses auto-generated component1()/component2()println("x=$x, y=$y")// Common in loops over mapsval scores = mapOf("Ana" to 90, "Bob" to 85)for ((name, score) in scores) { println("$name scored $score")}// Underscore to skip a component you don't need (Kotlin 1.1+)val (_, onlyY) = point
Auto-Generated Members
What the compiler generates for every data class.
- equals()/hashCode()- Structural equality based on all properties declared in the primary constructor
- toString()- Readable format: ClassName(prop1=value1, prop2=value2, ...)
- copy()- Creates a shallow copy with optionally overridden properties
- componentN()- One function per constructor property, enabling destructuring
- Only primary constructor properties- Properties declared in the class body (not the constructor) are excluded from these
Data Class Rules
Constraints the compiler enforces on data class declarations.
- Primary constructor required- Must have at least one parameter, all val or var
- Cannot be abstract/open/sealed/inner- Data classes cannot be extended in these ways
- val for immutability- Prefer val properties so equals/hashCode stay consistent over the object's lifetime
- Avoid mutable var as a map/set key- Mutating it after insertion breaks hashCode-based lookups
- Can implement interfaces- Data classes can implement interfaces and have extra members beyond the constructor
Pro Tip
Never use a `var` property in a data class as a HashMap/HashSet key — mutating it after insertion changes the hashCode, and the entry becomes unfindable even though `contains()` on the original reference still works.
Was this cheat sheet helpful?
Explore Topics
#KotlinDataClasses#KotlinDataClassesCheatSheet#Programming#Beginner#DeclaringDataClasses#TheCopyFunction#DestructuringDeclarations#AutoGeneratedMembers#OOP#Functions#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance