Kotlin Tutorial

Data Classes

Automatically generating toString, equals, and copy in Kotlin

What is a Data Class?

A Data Class is a class whose main purpose is to hold data. In Java, this usually requires writing dozens of lines of boilerplate (getters, setters, toString, equals, hashCode). In Kotlin, you just add the data keyword.
kotlin
data class User(val name: String, val age: Int)
Wait! With just one line, Kotlin gives you:
  1. equals() / hashCode(): Compares the data, not the memory address.
  2. toString(): Returns User(name=Alpha, age=25).
  3. copy(): Creates a copy with some modified values.
  4. componentN(): Allows for destructuring.

The copy() Function

Wait! Because data classes are often used as immutable models, the copy() function is incredibly useful for "changing" data safely.
kotlin
val user = User("Alpha", 25)
val olderUser = user.copy(age = 26) // Name stays "Alpha"

Destructuring Declarations

You can "unpack" a data class directly into variables.
kotlin
val (name, age) = user
println("$name is $age")

Requirements

To be a data class:
  • The primary constructor must have at least one parameter.
  • All primary constructor parameters must be marked as val or var.
  • Data classes cannot be abstract, open, or sealed.

Summary

TermDescription
dataKeyword that enables automatic method generation
copyCreating a modified clone of an object
equalsValue-based equality check
destructval (x, y) = obj syntax
Best ForModels, DTOs (Data Transfer Objects), and App State
Logic"Data is the focus"
SafetyPromotes immutability and predictable data handling
ModernReplaces heavy boilerplate with a single keyword
StandardUse data class for any class that just holds information
IdentityBased on the values inside the object
EfficiencyOptimized by the compiler to be as fast as manual code