-
Notifications
You must be signed in to change notification settings - Fork 24
Kotlin: Class vs DataClass
- Helps reduce boilerplate code
- Data classes are concise and designed for cases where the primary purpose is to hold and represent data.
Differences |
---|
Default Methods |
Comparing two instances |
Component Functions |
Copy Method |
No-arg Constructor |
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.
You would need to implement these methods manually.
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)
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")
}
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
There is no such feature using normal class
Provide a copy
method, which allows you to create a copy of an instance with some properties changed.
val modifiedPerson = person1.copy(age = 30)
Such a feature is not there
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)
This is not possible and you need to manually create it