There is a specific class in Kotlin, whose purpose is to hold the data of the object, that class is known as Kotlin data class. In this tutorial, you will learn all about the Kotlin data class with the help of some examples.
What Is a Data class in Kotlin?
The data class in Kotlin is used to hold the data. This data class contains some utility functions that are often derivable from the data.
With the help of the data class, you don’t need to write the boiler plate code. The compiler automatically generates all the getter and setter for all the data class properties. Its syntax includes the data keyword with class.
Now, go ahead and understand the requirement for the Kotlin data class.
Requirements of Data Class
You need to take care of some requirements while creating a data class.
- The primary constructor must have at least one parameter.
- The parameter of the primary constructor must be val or var.
- The data class cannot be abstract, sealed, or open.
- The data class can extend other classes and can implement the interface.
Now, have a look at the Functions of Kotlin data class.
Functions of Data Class
The data class includes some inbuilt functions, and due to these functions, you don’t need to write the boiler plate code. Following are those functions:
- toString()
- copy()
- equals()
- hashCode()
toString()
The data class has the toString() function, which can display the object's data just by printing the reference of the class.
The toString() function converts the object to a string and prints the string representation of the object. The syntax of the output will be in a particular format.
Now, go ahead and have a look at its example.
Here you have used the data class, and you know the data class contains the toString() function, which will convert the object to a string. So when you print the object p, the output would be as follows.
Now, you will understand the copy() function.
copy()
In data class, you can easily create a copy of an object with the help of the copy() function. This copy() function helps in altering the properties of the object.
As you can see, object r2 is copying the object r1, which means the properties of object r1 are getting copied to object r2. The object r3 is copying the properties of r1 and also altering or modifying the properties of r1. You can notice the roll number is getting altered.
Below is the output of the above example.
equals() and hashCode()
There are two more functions in the data class: equals() and hashCode(). The equal() function returns true or false; if two objects are equal, it will return true, else, it will return false.
Here, look at an example where equal() function is being used.
Below is the output of the above example.
The hashCode() function returns the hashcode of the object, i.e. if the objects are equal, then the hashcode will print the same integer output.
Below is the output of the above example.
Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!
Conclusion
In this tutorial on Kotlin data class, you understood what Kotlin Data class is, and learned the requirements to create the data class. You also learned about the Functions of data classes like toString(), copy(), hashCode(), etc, and understood them in detail with the help of examples.
If you are looking to build a software development career, you can check the Post-Graduate Program in Full Stack Development by Simplilearn. It can be the ideal solution to help you build your career in the right direction.
Do you have any questions regarding this tutorial on Kotlin Data class? If you do, then you can put them in the comments section. We’ll help you solve your queries.