Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Hey Ninjas!! Welcome to another article on Kotlin. Like Java, Kotlin also has its Data Class. Today we will learn about the Kotlin data class. We will also look into details of the methods of Kotlin Data Class.
Let's start with the basics of Kotlin Data Class.
Data Class in Kotlin
The Kotlin Data class is used for storing data in an organized form. The data class also ensures the reusability of data. The data keyword is used to declare a data class in Kotlin. Let's look at the syntax of Data Class in Kotlin.
Syntax:
data class employee(val name: String, val organization: String)
Here, employee is the name of the data class. The name and organisation are the variables of the data class used to store values.
Requirements of Kotlin Data Class
The data class in Kotlin must satisfy the following requirements:
The primary constructor of Kotlin Data Class must have at least one parameter.
All the primary constructor parameters must be val or var.
Data classes cannot be abstract, sealed, open, or inner.
Methods of Kotlin Data Class
The Kotlin Data Class automatically derives the following methods:
toString()
copy()
hashcode()
equals()
Let's look into the details of each of them.
toString()
The toString member of Kotlin data class is used to convert our data class objects value to string format. The data class object stores the values as hashcode (memory address).
Let's understand its implementation using the following Code:
Code:
data class employee(val name: String, val organization: String)
fun main() {
val emp = employee("Kumar", "Coding Ninjas")
println(emp.toString())
}
Output:
employee(name=Kumar, organization=Coding Ninjas)
Explanation:
The above code snippet prints all the elements of the emp variable of employee data class.
copy()
The copy() method of Kotlin data class is used to create a shallow copy of an object. This method helps in the reusability of an already-created object. We can easily change a few data of the already created object in our new one.
Let's understand its implementation using the following Code:
Code:
data class employee(val name: String, val organization: String)
fun main() {
val emp = employee("Kumar", "Coding Ninjas")
val emp1 = emp.copy(name = "Sinha");
println(emp.toString())
println(emp1.toString())
}
The above code snippet copies the emp object by changing the name to Sinha.
hashcode()
The hashcode() method of Kotlin data class returns the hashcode (memory address) value of the created data object.
Let's understand its implementation using the following Code:
Code:
data class employee(val name: String, val organization: String)
fun main() {
val emp = employee("Kumar", "Coding Ninjas")
val emp1 = employee("Singh", "Coding Ninjas")
println("Hashcode Value of emp = ${emp.hashCode()}")
println("Hashcode Value of emp1 = ${emp1.hashCode()}")
}
Output:
Hashcode Value of emp = 1296253835
Hashcode Value of emp1 = 1514240038
Explanation:
The above code prints the hashcode value of the object. Since both objects are independent of each other, they have different hashcode values.
equals()
The equals() method of the Kotlin data class is used to compare two data objects. The comparison is based on their hashcode value. It returns true if the data objects have the same hashcode value, else it returns false.
Let's understand its implementation using the following Code:
Code:
data class employee(val name: String, val organization: String)
fun main() {
val emp1 = employee("Kumar", "Coding Ninjas")
val emp2 = employee("Singh", "Coding Ninjas")
val emp3 = emp1.copy()
print("Comparing emp1 and emp2: ")
if( emp1.equals(emp2)){
println("emp1 is equal to emp2.")
}
else{
println("emp1 is not equal to emp2.")
}
print("Comparing emp1 and emp3: ")
if( emp1.equals(emp3)){
println("emp1 is equal to emp3.")
}
else{
println("emp1 is not equal to emp.")
}
}
Output:
Comparing emp1 and emp2: emp1 is not equal to emp2.
Comparing emp1 and emp3: emp1 is equal to emp3.
Explanation:
The above code snippet compares the objects emp1 with emp2 and emp1 with emp3. The object emp3 is a copy of emp1; they are equal.