Why does person1 == person2 return true in a data class? π€
In Kotlin, data classes automatically implement equals(), hashCode(), and toString().
Because of this, two instances of a data class with the same values are considered equal.
1οΈβ£ Understanding Equality in Kotlin
Kotlin has two types of equality checks:
- Structural Equality (
==) β Checks if values inside objects are the same.
- Referential Equality (
===) β Checks if two objects are the exact same instance in memory.
Example of Both Equality Types
fun main() {
val a = "Hello"
val b = "Hello"
val c = a
println(a == b) // β
true (same value)
println(a === b) // β
true (same memory, because Kotlin optimizes strings)
println(a === c) // β
true (c is literally the same object as a)
}
2οΈβ£ What Happens in a Normal (Non-Data) Class?
A regular class does NOT compare values automatically; it compares memory locations.
Example with a Normal Class
class NormalPerson(val firstName: String, val lastName: String, val age: Int)
fun main() {
val p1 = NormalPerson("Julio", "Batista", 25)
val p2 = NormalPerson("Julio", "Batista", 25)
println(p1 == p2) // β false (different memory locations)
}
Even though both p1 and p2 hold the same data, Kotlin compares their memory locations, which are different.
3οΈβ£ Why Does a data class Compare Values?
When you create a data class, Kotlin automatically generates:
β
equals() β Compares values instead of memory.
β
hashCode() β Allows using the object in sets/maps properly.
β
toString() β Prints the object nicely.
Example with data class
data class DataClassPerson(val firstName: String, val lastName: String, val age: Int)
fun main() {
val person1 = DataClassPerson("Julio", "Batista", 25)
val person2 = DataClassPerson("Julio", "Batista", 25)
println(person1 == person2) // β
true (same values)
println(person1 === person2) // β false (different memory locations)
}
π How It Works Internally
Kotlin auto-generates this equals() function for a data class:
override fun equals(other: Any?): Boolean {
if (this === other) return true // Same instance, return true
if (other !is DataClassPerson) return false // Not the same type, return false
return this.firstName == other.firstName &&
this.lastName == other.lastName &&
this.age == other.age
}
Since the values are the same, equals() returns true, making person1 == person2 true.
4οΈβ£ Summary π
| Feature |
Normal Class (class) |
Data Class (data class) |
| Compares values? |
β No (compares memory) |
β
Yes (compares values) |
Auto-generated equals()? |
β No |
β
Yes |
Auto-generated hashCode()? |
β No |
β
Yes |
Auto-generated toString()? |
β No |
β
Yes |
Key Takeaway
- Use
data class when you want objects to be compared by their values instead of memory.
- Use
class when identity matters more than content.
Would you like an even simpler explanation or a real-world analogy? π
Why does
person1 == person2returntruein adata class? π€In Kotlin, data classes automatically implement
equals(),hashCode(), andtoString().Because of this, two instances of a
data classwith the same values are considered equal.1οΈβ£ Understanding Equality in Kotlin
Kotlin has two types of equality checks:
==) β Checks if values inside objects are the same.===) β Checks if two objects are the exact same instance in memory.Example of Both Equality Types
2οΈβ£ What Happens in a Normal (Non-Data) Class?
A regular class does NOT compare values automatically; it compares memory locations.
Example with a Normal Class
Even though both
p1andp2hold the same data, Kotlin compares their memory locations, which are different.3οΈβ£ Why Does a
data classCompare Values?When you create a
data class, Kotlin automatically generates:β
equals()β Compares values instead of memory.β
hashCode()β Allows using the object in sets/maps properly.β
toString()β Prints the object nicely.Example with
data classπ How It Works Internally
Kotlin auto-generates this
equals()function for adata class:Since the values are the same,
equals()returns true, makingperson1 == person2true.4οΈβ£ Summary π
class)data class)equals()?hashCode()?toString()?Key Takeaway
data classwhen you want objects to be compared by their values instead of memory.classwhen identity matters more than content.Would you like an even simpler explanation or a real-world analogy? π