Skip to content

DataClassΒ #1

@JayCesar

Description

@JayCesar

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? πŸš€

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions