Data classes help you store data easily. Copying lets you make a new object with some changes. Destructuring helps you get parts of the data quickly.
Data class copy and destructuring in Kotlin
data class Person(val name: String, val age: Int) val person1 = Person("Anna", 25) // Copying val person2 = person1.copy(age = 30) // Destructuring val (name, age) = person1
The copy() function creates a new object with the same values, but you can change some.
Destructuring lets you assign properties to variables in one line.
data class Person(val name: String, val age: Int) val person1 = Person("Anna", 25) val person2 = person1.copy(age = 26)
val (name, age) = person1 println(name) println(age)
val person3 = person1.copy(name = "John") println(person3)
This program shows how to copy a data class object with a changed age and how to get its properties using destructuring.
data class Person(val name: String, val age: Int) fun main() { val person1 = Person("Anna", 25) val person2 = person1.copy(age = 30) val (name, age) = person2 println("Original: $person1") println("Copied: $person2") println("Destructured name: $name") println("Destructured age: $age") }
Copying does not change the original object.
Destructuring works because data classes automatically create component functions.
Data classes store data simply.
copy() makes a new object with some changes.
Destructuring extracts values into variables easily.
Practice
What does the copy() function do in a Kotlin data class?
Solution
Step 1: Understand the purpose of
Thecopy()copy()function in Kotlin data classes creates a new instance with the same property values.Step 2: Recognize optional property changes
You can specify new values for some properties while copying, leaving others unchanged.Final Answer:
Creates a new object with the same properties, optionally changing some. -> Option CQuick Check:
copy()= new object with changes [OK]
- Thinking copy() modifies the original object
- Confusing copy() with toString()
- Assuming copy() deletes the original
Which of the following is the correct syntax to copy a Kotlin data class Person and change only the age property?
data class Person(val name: String, val age: Int)
Solution
Step 1: Recall the copy() syntax
Thecopy()function uses named arguments to change specific properties.Step 2: Identify correct usage
Usingperson.copy(age = 30)changes only the age, keeping name same.Final Answer:
val newPerson = person.copy(age = 30) -> Option AQuick Check:
copy() uses named args for changes [OK]
- Passing values without property names
- Using copy() as a standalone function
- Changing wrong property by mistake
What is the output of this Kotlin code?
data class Point(val x: Int, val y: Int)
fun main() {
val p1 = Point(5, 10)
val (a, b) = p1
val p2 = p1.copy(y = 20)
println("a = $a, b = $b")
println(p2)
}Solution
Step 1: Understand destructuring of p1
Destructuring assignsa = p1.x = 5andb = p1.y = 10.Step 2: Understand copy() usage
p2is a copy ofp1withychanged to 20, soPoint(x=5, y=20).Final Answer:
a = 5, b = 10 Point(x=5, y=20) -> Option BQuick Check:
Destructuring + copy() = expected values [OK]
- Mixing up destructured values
- Assuming copy changes original
- Printing wrong variable
Find the error in this Kotlin code snippet:
data class User(val name: String, val age: Int)
fun main() {
val user1 = User("Alice", 25)
val (n, a) = user1.copy(age = 30)
println("Name: $n, Age: $a")
}Solution
Step 1: Check copy() usage
Thecopy()function is correctly called withage = 30.Step 2: Check destructuring
Destructuring works on the copy result, assigningn = "Alice"anda = 30.Final Answer:
No error; code runs and prints updated age. -> Option DQuick Check:
copy() + destructuring works fine [OK]
- Thinking destructuring fails on copy() result
- Believing copy() needs all properties
- Confusing variable names with property names
Given this data class and list, how do you create a new list of Person objects with age increased by 1, using copy() and destructuring?
data class Person(val name: String, val age: Int)
val people = listOf(Person("Tom", 20), Person("Sue", 25))Solution
Step 1: Understand list transformation
We want to create a new list with each person's age increased by 1.Step 2: Use copy() inside map()
Usingmapwithit.copy(age = it.age + 1)creates new objects with updated age.Final Answer:
people.map { it.copy(age = it.age + 1) } -> Option AQuick Check:
map + copy() = new list with changed ages [OK]
- Using forEach which returns Unit, not a list
- Trying to copy the whole list directly
- Ignoring destructuring or copy() usage
Ready to prove it?
Pass the test and this pattern moves unknown
