Kotlin Data class

Data class is used to hold the data information and this class also provide of some useful and standard functionality.

The main focus of this post understanding its basic concept and learning about how to use its functionality. data class is start with the data keyword. here is given of a syntax

data class className(variableType1 variableName1: DataType, variableType2 variableName2: DataType ..)

For example

data class Student(val id:Int ,var age:Int ,val name:String)
    
fun main(args: Array<String>){
   var obj1=Student(101,19,"Foo")
   var obj2=Student(102,21,"Bar")
   println("obj1 : $obj1")
   println("obj2 : $obj2")
}

Output

obj1 : Student(id=101, age=19, name=Foo)
obj2 : Student(id=102, age=21, name=Bar)

data class properties are used to similar of other class properties by object. But that is a small difference of compare to another class object. Normal class object are can not describing the information of its data member values and associative variable when it's printed. but data class is describe it primary constructor parameter variables and value. for example.

data class Student(val id:Int ,var age:Int ,val name:String)
class Employee(val id:Int ,var age:Int ,val name:String)  
fun main(args: Array<String>){
   var obj1=Student(101,19,"Foo")
   var obj2=Employee(102,21,"Bar")
   println("obj1 : $obj1")
   println("obj2 : ${obj2}")
}

Output

obj1 : Student(id=101, age=19, name=Foo)
obj2 : Employee@74a14482

Rule of data class

There is following requirements is compulsory to define data class.

Should be define at least one primary constructor parameter. because data class is based on data.

Primary constructor variables should be define by val and var.

Data classes cannot be abstract, open, sealed or inner

Method of data class

Copy : Before use of this method first view situation why use copy() method in data class. Suppose we are need to copy any data class properties. That clone object value are used to compare new updated records for pervious define object.

//define data class
data class info(var salary:Double)  
fun main(args: Array<String>){
   var obj1=info(80000.89)
   var obj2=obj1 //copy, of object reference
   println("Before obj1 : ${obj1.salary} obj2 : ${obj2.salary}")
   obj1.salary=90000.89
   println("After obj1 : ${obj1.salary} obj2 : ${obj2.salary}")
}

Output

Before obj1 : 80000.89 obj2 : 80000.89
After obj1 : 90000.89 obj2 : 90000.89

In this example we are try to clone (copy of object) to some class data object. but equal to operators is assigned the reference of obj1 to obj2. So clone not work when update any of obj1 or obj2 values.

The solution of this problem, we can used to copy() function of data class. This are copy all the properties of data class object to another object. And make new object. See this example.

data class Student(val id:Int ,var age:Int ,var name:String)  
fun main(args: Array<String>){
   var obj1=Student(101,19,"Foo")
   var obj2=obj1.copy()
   println("Before update")
   println("obj1 : $obj1")
   println("obj2 : ${obj2}")

   //update obj1 name value
   obj2.name="Bar"

   println("After update")
   println("obj1 : $obj1")
   println("obj2 : ${obj2}")
}

Output

Before update
obj1 : Student(id=101, age=19, name=Foo)
obj2 : Student(id=101, age=19, name=Foo)
After update
obj1 : Student(id=101, age=19, name=Foo)
obj2 : Student(id=101, age=19, name=Bar)

Here modified the name of obj2. that is an basic example of copy() function. Suppose we are copy an object properties with few new modification then do this way.

data class Info(var name:String,var address:String)
fun main(args: Array<String>){
   var obj1=Info("Foo","Anywhere")
   var obj2=obj1.copy(name="Bar") //change name 
   println("obj1 : $obj1")
   println("obj2 : $obj2")
}

Output

obj1 : Info(name=Foo, address=Anywhere)
obj2 : Info(name=Bar, address=Anywhere)

hashCode(): hash code is returning hashcode of an object. hashcode is 32 bit signed integer that is unique id allocated by JVM. when two or more object properties value are not same then hashcode is unique. Otherwise there is possible more than one object are hashcode is similar. See this example.

data class Info(var name:String,var address:String)
fun main(args: Array<String>){
   var obj1=Info("Data","Anywhere")
   var obj2=Info("Data","Anywhere") //same value of obj1
   var obj3=obj2.copy()
   var obj4=Info("Air","Anywhere")
  

   println("${obj1.hashCode()} ")
   println("${obj2.hashCode()} ")
   println("${obj3.hashCode()} ")
   println("${obj4.hashCode()} ")
}

Output

-197259247 
-197259247 
-197259247 
-261022031 

equals() : When if two object properties value are equal. Then equal() function is returning boolean result true. otherwise returning false.

data class Info(var name:String,var address:String)
fun main(args: Array<String>){
   var obj1=Info("Data","Anywhere")
   var obj2=Info("Data","Anywhere") //same value of obj1
   var obj3=obj2.copy()
   var obj4=Info("Air","Anywhere")
  

   println("${obj1.equals(obj2)} ")
   println("${obj2.equals(obj3)} ")
   println("${obj3.equals(obj4)} ")
   println("${obj4.equals(obj1)} ")
}

Output

true 
true 
false 
false 


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved