Kotlin Data Classes (original) (raw)

Last Updated : 19 Aug, 2019

We often create classes to hold some data in it. In such classes, some standard functions are often derivable from the data. In Kotlin, this type of class is known as data class and is marked as data.Example of a data :

data class Student(val name: String, val roll_no: Int)

The compiler automatically derives the following functions :

Rules to create Data classes -

Data classes have to fulfill the following requirements to ensure the consistency:

toString()

This function returns a string of all the parameters defined in the data class .Example:

Java `

fun main(args: Array) { //declaring a data class data class man(val roll: Int,val name: String,val height:Int)

//declaring a variable of the above data class 
//and initializing values to all parameters

val man1=man(1,"man",50)
 
//printing all the details of the data class
println(man1.toString());

}

`

Output:

man(roll=1, name=man, height=50)

**Note:**The compiler only uses the properties defined inside the primary constructor for the automatically generated functions. It excludes the properties that are declared in the class body.Example:

Java `

fun main(args: Array) { //declaring a data class data class man(val name: String) { //property declared in class body var height: Int = 0; }

//declaring a variable of the above data class and 
//initializing values to all parameters

val man1=man("manish")
//class body properties must be assigned uniquely
man1.height = 70
 
//this method prints the details of class that 
//are declared in primary constructor
println(man1.toString());

//printing the height of man1 
println(man1.height);

}

`

Output:

man(name=manish) 70

Here height is not used by the toString() function .

copy()

Sometimes we need to copy an object, with some change in some of its properties keeping all others unchanged. In this case copy() function is used.Properties of copy()

Declaration of copy()

fun copy(name: String = this.x, age: Int = this.y) = user(x, y)

where user is a data class : user(String, Int) .Example

Java `

fun main(args: Array) { //declaring a data class data class man(val name: String, val age: Int) { //property declared in class body var height: Int = 0; }

val man1 = man("manish",18)

//copying details of man1 with change in name of man
val man2 = man1.copy(name="rahul")

//copying all details of man1 to man3
val man3 = man1.copy();

//declaring heights of individual men
man1.height=100
man2.height=90
man3.height=110

//man1 & man3 have different class body values,
//but same parameter values

//printing info all 3 men
println("${man1} has ${man1.height} cm height")
println("${man2} has ${man2.height} cm height")
println("${man3} has ${man3.height} cm height")

}

`

Output:

man(name=manish, age=18) has 100 cm height man(name=rahul, age=18) has 90 cm height man(name=manish, age=18) has 110 cm height

hashCode() and equals()

Declaration of hashCode() :

open fun hashCode(): Int

Properties of hashCode()

fun main(args: Array) { //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

Explanation: man1 and man2 have same object contents, so they are equal, thus they have same hash code values.