0
0
Kotlinprogramming~10 mins

Data class copy and destructuring in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Data class copy and destructuring
Create data class instance
Destructure instance into variables
Use copy() to create modified instance
Compare original and copied instances
End
This flow shows creating a data class object, breaking it into variables, copying it with changes, and comparing both objects.
Execution Sample
Kotlin
data class User(val name: String, val age: Int)

fun main() {
  val user1 = User("Alice", 30)
  val (name, age) = user1
  val user2 = user1.copy(age = 31)
  println("$name is $age years old")
  println(user2)
}
Creates a User, destructures it, copies with a changed age, then prints values.
Execution Table
StepActionEvaluationResult
1Create user1User("Alice", 30)user1 = User(name=Alice, age=30)
2Destructure user1(name, age) = user1name = "Alice", age = 30
3Copy user1 with age=31user1.copy(age=31)user2 = User(name=Alice, age=31)
4Print name and ageprintln("$name is $age years old")Output: Alice is 30 years old
5Print user2println(user2)Output: User(name=Alice, age=31)
6EndNo more codeProgram ends
💡 Program ends after printing both original and copied user details.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
user1undefinedUser(name=Alice, age=30)User(name=Alice, age=30)User(name=Alice, age=30)User(name=Alice, age=30)
nameundefinedundefined"Alice""Alice""Alice"
ageundefinedundefined303030
user2undefinedundefinedundefinedUser(name=Alice, age=31)User(name=Alice, age=31)
Key Moments - 2 Insights
Why does the destructured 'age' variable stay 30 even after copying user1 with age 31?
Because destructuring happens before the copy. The variable 'age' holds the original value from user1 (30), not from the copied user2.
Does the copy() function change the original user1 object?
No, copy() creates a new object with the specified changes. user1 remains unchanged as shown in step 3 and variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 3. What is the value of user2?
AUser(name=Alice, age=30)
BUser(name=Alice, age=31)
CUser(name=Bob, age=31)
DUser(name=Bob, age=30)
💡 Hint
Check the 'Result' column at step 3 in the execution_table.
At which step does the variable 'name' get assigned the value "Alice"?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Action' and 'Result' columns for when destructuring happens.
If we changed user1.copy(age = 35) instead of 31, what would user2's age be in the variable_tracker after step 3?
A35
B31
C30
DUndefined
💡 Hint
The copy() function sets the new age value in user2 as shown in step 3.
Concept Snapshot
Data class copy and destructuring in Kotlin:
- Use 'val (a, b) = obj' to unpack properties.
- Use 'obj.copy(prop = newVal)' to create modified copies.
- Original object stays unchanged.
- Destructured variables keep original values.
- Useful for easy object cloning and unpacking.
Full Transcript
This example shows how Kotlin data classes let you easily copy objects and break them into variables. First, we create a User named Alice aged 30. Then we unpack her name and age into separate variables. Next, we make a copy of Alice but change her age to 31. When printing, the destructured age is still 30 because it was taken before copying. The copy creates a new User with the updated age, leaving the original unchanged. This helps manage data cleanly and simply.