data class
A Kotlin data class is a class designed specifically for holding data. It automatically generates equals(), hashCode(), toString(), copy(), and componentN() methods. It is ideal for implementing value objects and DTOs.
Syntax
// Defining a data class (the primary constructor must have at least one property) data class Point(val x: Int, val y: Int) // Creating an instance val p = Point(3, 4) // Auto-generated methods p.toString() // "Point(x=3, y=4)" p.copy(x = 10) // Point(x=10, y=4) val (x, y) = p // Destructuring declaration // equals performs value comparison. Point(1, 2) == Point(1, 2) // true
Method List
| Method / Feature | Description |
|---|---|
| toString() | Returns a string in the format "ClassName(property=value, ...)". |
| equals(other) | Compares all property values (used by the == operator). |
| hashCode() | Returns a hash code based on the property values. |
| copy(propertyName = value) | Creates a new instance with some properties changed. |
| component1(), component2(), ... | Auto-generated methods used by destructuring declarations (val (a, b) = obj). |
Sample Code
// Defining data classes
data class User(
val id: Int,
val name: String,
val email: String,
val age: Int = 0 // Default values are supported.
)
data class Point(val x: Double, val y: Double) {
// You can add methods to a data class.
fun distanceTo(other: Point): Double {
val dx = x - other.x
val dy = y - other.y
return Math.sqrt(dx * dx + dy * dy)
}
}
fun main() {
val user1 = User(1, "Alice", "alice@example.com", 30)
val user2 = User(1, "Alice", "alice@example.com", 30)
val user3 = User(2, "Bob", "bob@example.com")
// toString() — useful for debugging
println(user1) // User(id=1, name=Alice, email=alice@example.com, age=30)
// equals() — value comparison
println(user1 == user2) // true (same values)
println(user1 == user3) // false
// copy() — change only some properties
val updatedUser = user1.copy(age = 31, email = "alice_new@example.com")
println(updatedUser)
println(user1) // The original is unchanged (immutable)
// Destructuring declaration (componentN)
val (id, name, email) = user1
println("ID: $id, Name: $name, Email: $email")
// Using data classes in a list
val users = listOf(user1, user3, User(3, "Carol", "carol@example.com", 25))
val sortedUsers = users.sortedBy { it.name }
sortedUsers.forEach { println("${it.name} (age: ${it.age})") }
// Calling a method on Point
val p1 = Point(0.0, 0.0)
val p2 = Point(3.0, 4.0)
println("Distance: ${p1.distanceTo(p2)}") // 5.0
// Copying with a partial change
val p3 = p1.copy(x = 1.0)
println("Copy: $p3") // Point(x=1.0, y=0.0)
}
Notes
data class is extremely useful in Kotlin for implementing value objects in DDD (Domain-Driven Design) and response models for APIs. Code that would take nearly 100 lines in Java can be written in a single line.
Note: The primary constructor of a data class must have at least one property. Also, data classes are not automatically open, so they cannot be used as base classes for inheritance (use a regular class if you need inheritance).
For the basic class structure, see class / Constructor. For inheritance with type restrictions, see sealed class / sealed interface.
If you find any errors or copyright issues, please contact us.