A Data Class is just like any other Kotlin class which comes with some added functionality.
A Data Class already has built in functions that makes your life a lot easier.
They are :
Now, before we understand the usage of each Function, let us see the conditions to create a Data Class.
Now, let us see, how can we define a Data Class.
data class Human(var name: String, var food: String, var language: String) fun main() { var human = Human("Rakhi", "Rice", "Hindi") println(human.name+" eats "+human.food+" and speaks "+human.language) }
So, in the above code, we have defined a data class,
data class Human(var name: String, var food: String, var language: String)
And yes, it is that simple.
It begins with a data keyword and there is no body in it.
Now, let us see the use of the functions it supports by default.
Using the copy() Function, we can make an exact copy of an object.
data class Human(var name: String, var food: String, var language: String) fun main() { var human1 = Human("Rakhi", "Rice", "Hindi") println("Before Copying") println(human1.name+" eats "+human1.food+" and speaks "+human1.language) var human2 = human1.copy() println("After Copying") println(human2.name+" eats "+human2.food+" and speaks "+human2.language) }
So, in the above code, we have defined an object human1,
var human1 = Human("Rakhi", "Rice", "Hindi")
And made a copy of it using the copy() Function(Already defined in the data class),
var human2 = human1.copy()
And initialised to human2 object.
In order to check if two objects has equal values, it is essential to override/redefine the hashCode() and equals() Function.
But thanks to the data class. The data class has the hashCode() and equals() Function defined in it.
data class Human(var name: String, var food: String, var language: String) fun main() { var human1 = Human("Rakhi", "Rice", "Hindi") var human2 = Human("Rakhi", "Rice", "Hindi") if(human1.equals(human2)) println("Two objects are equal") else println("Two objects are not equal") }
So, in the above code, we have defined two objects,
var human1 = Human("Rakhi", "Rice", "Hindi") var human2 = Human("Rakhi", "Rice", "Hindi")
With equal values.
And when we use the equals() method to check if two objects are equal or not.
if(human1.equals(human2)) println("Two objects are equal") else println("Two objects are not equal")
Now, since two objects are equal, we get the output,
Two objects are equal
So, you might be thinking, if two objects are equal, the equals() method should return that the objects are equal.
Well! It's not the case.
Let us try the same example using a class(Not data class).
class Human(var name: String, var food: String, var language: String){ } fun main() { var human1 = Human("Rakhi", "Rice", "Hindi") var human2 = Human("Rakhi", "Rice", "Hindi") if(human1.equals(human2)) println("Two objects are equal") else println("Two objects are not equal") }
Well, in this case we got the output as,
Two objects are not equal
That is because we have declared a simple class,
class Human(var name: String, var food: String, var language: String){ }
Which doesn't have a hashCode() and equals() method defined.
The toString() Function displays the contents of an object in a structured manner.
data class Human(var name: String, var food: String, var language: String) fun main() { var human1 = Human("Rakhi", "Rice", "Hindi") println(human1.toString()) }
So, if you look at the output,
Human(name=Rakhi, food=Rice, language=Hindi)
You can see that the toString() Function,
println(human1.toString())
Displays the contents of an object in a structured way.