Android ● ● ○ Kotlin 2.3

Kodebits Day 11: Data Class Copy

Apr 24 2026
Use copy() to derive immutable variants in Kotlin.

What is the output?

data class User(
  val name: String,
  val points: Int
)
fun main() {
  val a = User("Ana", 3)
  val b = a.copy(points = 8)
  print(a.points)
  print(", ")
  println(b.points)
}


Try it in the online Kotlin Playground →

[spoiler title="Solution"]

Answer:

3, 8

Explanation:

copy creates a new object. a stays unchanged while b has updated points.

[/spoiler]


Further Reading