Data classes, equality and copying
Data classes, equality and copying
A report needs to compare two records by their data rather than by their object identity. A data class generates useful operations from the properties declared in its primary constructor, including equals, hashCode, toString, component functions and copy.
What you’ll learn
- Compare data-class equality and identity.
- Recognize the shallow-copy boundary.
Data classes, equality and copying in the example
Original record
A report needs to compare two records by their data rather than by their object identity.
Original recordCopy changed score
Copy creates another instance with specified constructor values changed.
Copy changed scoreCompare value and identity
The original remains 70 and the new instance holds 80.
Compare value and identity
Understand data classes, equality and copying
Copy creates another instance with specified constructor values changed. It is a shallow copy: a property referring to a mutable object can remain shared. Properties declared only in the body are not included in the generated primary-constructor equality contract.
Connect this to classes, objects and shared references and read-only and mutable lists.
Follow data classes, equality and copying
Save the complete source as Main.kt and use the Kotlin/JVM environment. No separate input file is needed.
Kotlin/JVM · complete program
data class Student(val name: String, val score: Int)
fun main() {
val original = Student("Diya", 70)
val corrected = original.copy(score = 80)
println(original.score)
println(corrected.score)
println(original == Student("Diya", 70))
println(original === corrected)
}Expected output
70
80
true
falseFollow the execution
The original remains 70 and the new instance holds 80. A separately constructed equal record compares true with ==, while the copied record has a different identity. Generated equality and identity answer different questions.
Try this in the Kotlin compiler by pasting the complete program.
Practice: Data classes, equality and copying
Your task: Does copy deeply clone a mutable list stored as a property?
Kotlin/JVM · complete program
data class Student(val name: String, val score: Int)
fun main() {
val original = Student("Diya", 70)
val corrected = original.copy(score = 80)
println(original.score)
println(corrected.score)
println(original == Student("Diya", 70))
println(original === corrected)
}Worked answer
No. Copy is shallow, so a referenced list can remain shared unless you explicitly create appropriate independent data.
Data classes, equality and copying: the important boundary
Mutable equality or hash-relevant fields can cause problems if an object is used as a set element or map key and then changed. Prefer stable key values and document which fields define record equality.
Check your understanding
Check the practical consequence
Data classes, equality and copying: what to keep in mind
No. Copy is shallow, so a referenced list can remain shared unless you explicitly create appropriate independent data. Mutable equality or hash-relevant fields can cause problems if an object is used as a set element or map key and then changed.
Further reading: official Kotlin documentation for this topic.
Reaching the recap and completing the quizzes are separate milestones.