intermediate15 min

Data Classes

Kotlin's data class: automatic equals, hashCode, toString, and copy.

What you'll learn

  • Declare a data class and explain what it automatically generates
  • Use the automatically-generated toString() and equals() to compare and print instances
  • Use copy() to create a modified copy of a data class instance

Explanation

A data class is Kotlin's concise way to declare a class whose primary purpose is holding data: data class Point(val x: Int, val y: Int). Adding the data keyword automatically generates several useful methods that you'd otherwise have to write by hand: a meaningful toString() (e.g. Point(x=3, y=4) instead of a memory address), equals()/hashCode() based on the properties (so two Point instances with the same x/y are considered equal), and copy().

copy() creates a new instance with some properties changed and the rest kept: val moved = point.copy(x = 10) creates a new Point with x = 10 and the original y unchanged -- especially useful alongside val properties, since you can't mutate the original but can cheaply produce a modified copy.

Data classes are commonly used for simple value-holding types: API response models, coordinates, configuration objects -- anywhere the main job of the class is "hold these values and compare/print them sensibly."

Guided lab

Predict: A data class's generated toString, equals, and copy

KotlinNot executed
This lab does not run in your browser or on VisaSparkSchools's servers. Read the code, predict what it does, then reveal the real expected output.

Read this program and predict exactly what it prints.

data class Point(val x: Int, val y: Int)

fun main() {
    val a = Point(3, 4)
    val b = Point(3, 4)
    val c = a.copy(x = 10)

    println(a)
    println(a == b)
    println(c)
}

Stuck? Get a hint.

Common mistakes

  • Writing a regular class and manually implementing equals()/hashCode()/toString() when a data class would generate all three automatically.
  • Trying to mutate a data class's val properties directly instead of using copy() to produce a new instance with changes.
  • Assuming two data class instances with identical property values are the same object reference -- equals() compares values, not identity (though it also makes such comparisons return true, which is the intended behavior).

Knowledge check

Knowledge check

1. What does adding `data` before `class Point(...)` automatically generate?
2. What does `point.copy(x = 10)` do?
3. How does a data class's generated equals() compare two instances?

Takeaway

Use `data class` for value-holding types to get toString/equals/hashCode/copy for free, and use copy() instead of trying to mutate val properties.

Summary

Data classes automatically generate toString, value-based equals/hashCode, and copy -- ideal for simple value-holding types like API models or coordinates.

References

Your notes

Notes save automatically.

Finished this lesson?

Mark it complete to track your progress and schedule a future review.