Kotlin Data Class: In this tutorial, you will learn to create data classes in Kotlin. You will likewise learn about requirements that data class should fulfill and their standard functionalities.
Data class is a basic class that is used to hold data/state and contains standard usefulness. A data catchphrase is used to declare a class as a data class.
There may arise a circumstance where you need to create a class exclusively to hold data. In such cases, you can check the class as data to create a data class. For instance,
data class Person(val name: String, var age: Int)
For this class, the compiler consequently produces:
copy() function, equivalents() and hashCode() pair, and toString() type of the essential constructor
componentN() functions
Prior to discussing these highlights in detail, let’s talk about requirements that a data class must fulfill.
Contents
Kotlin Data Class Requirements
Here are the prerequisites:
- The essential constructor should have at least one parameter.
- The parameters of the essential constructor should be set apart as either val (read-just) or var (read-write).
- The class can’t be open, abstract, inner, or sealed.
- The class may extend different classes or carry out interfaces. On the off chance that you are using the Kotlin variant before 1.1, the class can just implement interfaces.
Example: Kotlin Data Class
data class User(val name: String, val age: Int) fun main(args: Array<String>) { val salman = User("salman", 29) println("name = ${salman.name}") println("age = ${salman.age}") }
At the point when you run the program, the output will be:
name = salman age = 29
At the point when you pronounce a data class, the compiler automatically creates a few functions like toString(), equals(), hashcode() and so on in the background. This assists with keeping you code compact. Had you used Java, you would have to write a ton of standard code.
Let’s use these functions:
Copying
For a data class, you can create a copy of an object with a portion of its properties distinctive using copy() work. Here’s the way it works:
data class User(val name: String, val age: Int) fun main(args: Array<String>) { val u1 = User("Sohail", 29) // using copy function to create an object val u2 = u1.copy(name = "Haroon") println("u1: name = ${u1.name}, name = ${u1.age}") println("u2: name = ${u2.name}, name = ${u2.age}") }
At the point when you run the program, the output will be:
u1: name = Sohail, name = 29 u2: name = Haroon, name = 29
toString() method
The toString() function returns a string representation of the object.
data class User(val name: String, val age: Int) fun main(args: Array<String>) { val u1 = User("Sohail", 29) println(u1.toString()) }
At the point when you run the program, the output will be:
User(name=Sohail, age=29)
hashCode() and equivalents()
The hasCode() strategy returns hash code for the object. On the off chance that two objects are equivalent, hashCode() produces a similar integer outcome. Suggested Reading: hashCode()
The equivalents() returns true if two objects are equivalent (has same hashCode()). In the event that items are not equivalent, equals() returns false. Suggested Reading: equals()
data class User(val name: String, val age: Int) fun main(args: Array<String>) { val u1 = User("Sohail", 29) val u2 = u1.copy() val u3 = u1.copy(name = "muskan") println("u1 hashcode = ${u1.hashCode()}") println("u2 hashcode = ${u2.hashCode()}") println("u3 hashcode = ${u3.hashCode()}") if (u1.equals(u2) == true) println("u1 is equal to u2.") else println("u1 is not equal to u2.") if (u1.equals(u3) == true) println("u1 is equal to u3.") else println("u1 is not equal to u3.") }
At the point when you run the program, the output will be:
u1 hashcode = 71750738 u2 hashcode = 71750738 u3 hashcode = 771732263 u1 is equal to u2. u1 is not equal to u3.
Destructuring Declarations
You can destructure an object into various variables using destructuring declaration. For instance:
data class User(val name: String, val age: Int, val gender: String) fun main(args: Array<String>) { val u1 = User("Sohail", 29, "Male") val (name, age, gender) = u1 println("name = $name") println("age = $age") println("gender = $gender") }
At the point when you run the program, the output will be:
name = Sohail age = 29 gender = Male
This was conceivable on the grounds that the compiler creates componentN() works all properties for a data class. For instance:
data class User(val name: String, val age: Int, val gender: String) fun main(args: Array<String>) { val u1 = User("Sohail", 29, "Male") println(u1.component1()) // Sohail println(u1.component2()) // 29 println(u1.component3()) // "Male" }
At the point when you run the program, the output will be:
Sohail 29 Male
Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.
