0
0
Kotlinprogramming~10 mins

Data classes for value holders in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Data classes for value holders
Define data class with properties
Create instance of data class
Access properties directly
Use built-in functions: toString(), equals(), copy()
Modify instance with copy() if needed
Use instance as value holder
Data classes hold values with auto-generated useful functions like toString, equals, and copy for easy use.
Execution Sample
Kotlin
data class User(val name: String, val age: Int)

fun main() {
  val user1 = User("Alice", 30)
  println(user1)
  val user2 = user1.copy(age = 31)
  println(user2)
}
Defines a User data class, creates an instance, prints it, copies it with a changed age, and prints the new instance.
Execution Table
StepActionEvaluationResult
1Define data class UserUser class with properties name:String, age:IntClass ready for instances
2Create user1 = User("Alice", 30)Call constructoruser1 = User(name="Alice", age=30)
3Print user1Call user1.toString()Output: User(name=Alice, age=30)
4Create user2 = user1.copy(age=31)Copy user1 with age changeduser2 = User(name="Alice", age=31)
5Print user2Call user2.toString()Output: User(name=Alice, age=31)
6End of mainProgram endsExecution stops
💡 Program ends after printing user2
Variable Tracker
VariableStartAfter Step 2After Step 4Final
user1undefinedUser(name="Alice", age=30)User(name="Alice", age=30)User(name="Alice", age=30)
user2undefinedundefinedUser(name="Alice", age=31)User(name="Alice", age=31)
Key Moments - 3 Insights
Why does printing user1 show User(name=Alice, age=30) instead of a memory address?
Because data classes automatically generate a toString() method that shows property names and values, as seen in execution_table step 3.
What does the copy() function do in step 4?
It creates a new instance copying all properties from user1 but allows changing specified properties like age, shown in execution_table step 4.
Are user1 and user2 the same object after copying?
No, copy() creates a new object with the same or changed values; user1 and user2 are different instances as tracked in variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is printed when user1 is printed?
AAlice, 30
BUser@12345
CUser(name=Alice, age=30)
DError
💡 Hint
Check the Result column in execution_table step 3 for the printed output.
At which step is a new User instance created with age 31?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Look at the Action and Result columns in execution_table step 4.
If we change user2's name to "Bob" in copy(), what changes in variable_tracker after step 4?
Auser1 will have name="Bob"
Buser2 will have name="Bob" and age=31
Cuser1 and user2 will be identical
Duser2 will have name="Alice" and age=30
💡 Hint
copy() creates a new instance with changed properties, see variable_tracker and step 4.
Concept Snapshot
Data classes hold values with automatic functions.
Syntax: data class Name(val prop: Type)
Auto-generates toString(), equals(), hashCode(), copy().
Use copy() to create modified copies.
Ideal for simple value holders.
Full Transcript
This example shows how Kotlin data classes work as value holders. First, a data class User is defined with two properties: name and age. When we create an instance user1 with name "Alice" and age 30, Kotlin automatically provides a toString() method that prints the object nicely as User(name=Alice, age=30). Then, using the copy() function, we create user2 from user1 but change the age to 31. This creates a new object with the updated age, leaving user1 unchanged. Printing user2 shows the updated values. Data classes simplify handling data by providing useful functions automatically.