copy and equals
Generated functions.
copy and equals is a free Kotlin Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Generated Functions
Beyond toString, a data class generates equals, hashCode, and copy.
These three make data classes a joy to compare and clone.
Structural Equality
The generated equals compares objects by their property values, not their identity.
Two data class instances with the same values are equal with ==.
data class User(val name: String, val age: Int)
fun main() {
val a = User("Alice", 30)
val b = User("Alice", 30)
println(a == b)
}Different Values Are Not Equal
Change any property and the objects are no longer equal.
Equality is computed from every property in the primary constructor.
data class User(val name: String, val age: Int)
fun main() {
val a = User("Alice", 30)
val b = User("Alice", 31)
println(a == b)
}Equality vs Identity
Use == for value (structural) equality and === for reference (identity) equality.
Two equal data objects can be different instances in memory.
data class User(val name: String)
fun main() {
val a = User("Alice")
val b = User("Alice")
println(a == b) // values match
println(a === b) // different instances
}Consistent hashCode
hashCode is generated to match equals: equal objects produce the same hash.
This is what makes data classes work correctly in sets and as map keys.
data class Point(val x: Int, val y: Int)
fun main() {
val a = Point(1, 2)
val b = Point(1, 2)
println(a.hashCode() == b.hashCode())
}The copy Function
copy creates a new object that is identical to the original.
With immutable val properties, copying is how you make a modified version.
data class User(val name: String, val age: Int)
fun main() {
val a = User("Alice", 30)
val b = a.copy()
println(b)
println(a == b)
}Copy with Changes
Pass named arguments to copy to change specific properties while keeping the rest.
This is the standard pattern for updating immutable data.
data class User(val name: String, val age: Int)
fun main() {
val a = User("Alice", 30)
val older = a.copy(age = 31)
println(a)
println(older)
}Copy Leaves the Original Intact
copy never mutates the source object. The original keeps its values.
This immutability makes state changes predictable and safe.
data class Account(val owner: String, val balance: Int)
fun main() {
val original = Account("Sam", 100)
val updated = original.copy(balance = 150)
println(original.balance)
println(updated.balance)
}Chaining Copies
Because copy returns a new object, you can apply several updates in sequence.
Each step produces a fresh, independent value.
data class Config(val volume: Int, val muted: Boolean)
fun main() {
val base = Config(50, false)
val result = base.copy(volume = 70).copy(muted = true)
println(result)
}Only Constructor Properties Count
Just like equality, copy only handles primary-constructor properties.
Body properties are reset to their defaults in the copy, so keep important state in the constructor.
data class User(val name: String) {
var note: String = "none"
}
fun main() {
val a = User("Alice")
a.note = "VIP"
val b = a.copy()
println(b.note) // back to default
}Putting It Together
The generated functions give data classes their power:
equalsand==compare by valuehashCodestays consistent with equalscopyclones with optional changes
data class User(val name: String, val age: Int)
fun main() {
val a = User("Alice", 30)
val b = a.copy(name = "Alicia")
println(a == b)
println(b)
}Quick Check
Test your understanding of copy and equals.
Recap
You learned the generated functions of data classes:
- equals compares by value,
==vs=== - hashCode stays consistent for collections
- copy clones with selective changes
Next you will learn how destructuring unpacks objects into variables.
Frequently asked questions
Is the “copy and equals” lesson free?
Yes — the full text of “copy and equals” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.
What will I learn in “copy and equals”?
Generated functions. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Kotlin Academy?
No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “copy and equals” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Kotlin Academy lesson?
Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.