We sometimes create classes to hold some data in it. These classes have, some functions which are drived from the data. This type of class is known as data class in kotlin.
Data class internally contains the following functions:
- equals(): Boolean
- hashCode(): Int
- toString(): String
- copy()
1:-Kotlin Data Class equals() and hashCode():
The hashCode() method returns hash code for the object. If two objects are equal, hashCode()produces the same integer result and, equals() returns true if the hashCode() is equal, else it returns a false.
fun main(args: Array<String>)
{
//declaring a data class
data class man(val name: String, val age: Int)
val man1 = man("manish",18)
val man2 = man1.copy(name="rahul")
val man3 = man1.copy();
val hash1=man1.hashCode();
val hash2=man2.hashCode();
val hash3=man3.hashCode();
println(hash1)
println(hash2)
println(hash3)
//checking equality of these hash codes
println("hash1 == hash 2 ${hash1.equals(hash2)}")
println("hash2 == hash 3 ${hash2.equals(hash3)}")
println("hash1 == hash 3 ${hash1.equals(hash3)}")
}
Output:
835510190
-938448478
835510190
hash1 == hash 2 false
hash2 == hash 3 false
hash1 == hash 3 true
2:-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("John", 29)
println(u1.toString())
}
Output:
User(name=John, age=29)
3:-Copy:
You can make a copy of an object with some of its different properties using copy() function. Here’s how it works:
data class User(val name: String, val age: Int)
fun main(args: Array<String>) {
val u1 = User("John", 29)
// using copy function to create an object
val u2 = u1.copy(name = "Randy")
println("u1: name = ${u1.name}, name = ${u1.age}")
println("u2: name = ${u2.name}, name = ${u2.age}")
}
output:
u1: name = John, name = 29
u2: name = Randy, name = 29
Requirements of data class:
- its primary constructor must have at least one parameter.
- Parameters of primary constructor marked as val or var.
- Data class can not be inner,sealed, open or abstract.
- Data classes only implement interfaces.
Difference between Java data class and Kotlin data class:
If we want to create a entry of User in Java using data class, it require lots of boilerplate code.
public class User {
private String name;
private int rollno;
private String email;
public User(String name, int rollno, String email) {
this.name = name;
this.rollno = rollno;
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public intgetRollno() {
return id;
}
public void setRollno(int rollno) {
this.rollno = rollno;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof User)) return false;
User user = (User) o;
return getRollno() == user.getRollno() &&
Objects.equals(getName(), user.getName()) &&
Objects.equals(getEmail(), user.getEmail());
}
@Override
public inthashCode() {
return Objects.hash(getName(), getRollno(), getEmail());
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", rollno =" + rollno +
", email='" + email + '\'' +
'}';
}
}
Calling the constructor of above Java data class using the object of User class as
class MyClass{
public static void main(String agrs[]){
User u = new User("Ashu",101,"mymail@mail.com");
System.out.println(u);
}
}
Output:-
User{name='Ashu', rollno =101, email='mymail@mail.com'}
This Java data class code is written in Kotlin data code as:
data class User(var name: String, var rollno: Int, var email: String)
fun main(agrs: Array<String>) {
val u = User("Ashu", 101, "mymail@mail.com")
println(u)
}
Output:-
User(name=Ashu, rollno =101, email=mymail@mail.com)