Skip to content

Kotlin: Class vs DataClass

Devrath edited this page Jan 26, 2024 · 11 revisions

Summary

  • Helps reduce boilerplate code
  • Data classes are concise and designed for cases where the primary purpose is to hold and represent data.

Key differences between a normal class and a data class

Default Methods

Data classes:

They automatically generate some commonly used methods like toString(), equals(), hashCode(), and copy() for you, whereas in a normal class, you would need to manually implement these methods.

Normal classes:

You would need to implement these methods manually.

Comparing two instances

Data classes:

Comparing two instances based on the data it holds is simple

   data class Person(val name: String, val age: Int)

   val person1 = Person("John", 25)
   val person2 = Person("John", 25)

   println(person1 == person2) // true (equals() is automatically generated)

Normal classes:

Here we need to implement our own custom comparator or using equals to achieve the same

Using equals

class PersonNormalClass(var name: String, var age: Int) {
    // Override the equals method for custom comparison
    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (other == null || javaClass != other.javaClass) return false

        // Check for equality based on name and age
        val otherPerson = other as PersonNormalClass
        return name == otherPerson.name && age == otherPerson.age
    }

    // Override the hashCode method to be consistent with the equals method
    override fun hashCode(): Int {
        var result = name.hashCode()
        result = 31 * result + age
        return result
    }
}

fun main() {
    // Create two instances of PersonNormalClass
    val person1 = PersonNormalClass("John", 25)
    val person2 = PersonNormalClass("John", 25)

    // Compare instances using equals method
    val areEqual = person1 == person2
    println("Are the instances equal: $areEqual")
}

Using comparator

import java.util.Comparator

class PersonComparator : Comparator<PersonNormalClass> {
    override fun compare(person1: PersonNormalClass, person2: PersonNormalClass): Int {
        // Compare based on name and then age
        val nameComparison = person1.name.compareTo(person2.name)
        if (nameComparison != 0) {
            return nameComparison
        }
        return Integer.compare(person1.age, person2.age)
    }
}

fun main() {
    // Create two instances of PersonNormalClass
    val person1 = PersonNormalClass("John", 25)
    val person2 = PersonNormalClass("John", 25)

    // Use the Comparator to compare instances
    val comparator = PersonComparator()
    val comparisonResult = comparator.compare(person1, person2)

    // Print the result of the comparison
    println("Comparison result: $comparisonResult")
}

Component Functions

Data classes:

Automatically provide component functions for properties, which can be useful in certain scenarios, like destructuring declarations.

   val (name, age) = person1 // Destructuring declaration using component functions

Normal classes:

There is no such feature using normal class

Copy Method

Data classes:

Provide a copy method, which allows you to create a copy of an instance with some properties changed.

   val modifiedPerson = person1.copy(age = 30)

Normal classes:

Such a feature is not there

No-arg Constructor

Data classes:

Data classes automatically generate a no-arg constructor if all the properties have default values.

   data class Student(val name: String = "", val age: Int = 0)

Normal classes:

This is not possible and you need to manually create it

Clone this wiki locally