Kotlin Data classData class is a simple class which is used to hold data/state and contains standard functionality. A data keyword is used to declare a class as a data class. Declaring a data class must contains at least one primary constructor with property argument (val or var). Data class internally contains the following functions:
Due to presence of above functions internally in data class, the data class eliminates the boilerplate code. A compression between Java data class and Kotlin data classIf we want to create a User entry in Java using data class, it require lots of boilerplate code. Calling the constructor of above Java data class using the object of User class as Output: User{name='Ashu', id=101, email='[email protected]'} The above Java data class code is rewritten in Kotlin data code in single line as Calling the constructor of above Kotlin data class using the object of User class as Output: User(name=Ashu, id=101, [email protected]) Requirements of data classIn order to create a data class, we need to fulfill the following requirements:
Kotlin data class toString() methodsKotlin data class only focuses on data rather than code implementation. Let's see a simple program without data class. In this class, we are trying to print the reference of Product class using its object. While printing the reference of Product class, it displays the hashCode() with class name of Product. It does not print the data. Output: Product@266474c2 The above program is rewritten using data class and printing the reference of Product class and displaying the data of object. It happens because the data class internally contains the toString() which display the string representation of object . Output: Product(name=laptop, price=25000) Kotlin data classequals() and hashCode()The equal() method is used to check other object is "equal to" current object. While doing comparison between two or more hashCode(), equals() method returns true if the hashCode() are equal, else it returns a false. For example, let's see an example in which a normal class comparing the two references of same class Product having same data. In above program, reference p1 and reference p2 has different references. Due to different reference values in p1 and p2, doing comparison displays false. Output: false false The above program is rewritten using data class, printing the reference of Product class and displaying the data of object. The hashCode() method returns hash code for the object. The hashCode() produce same integer result, if two objects are equal. Output: true true Kotlin data class copy() methodThe data class provides a copy() method which is used to create a copy (or colon) of object. Using copy() method, some or all properties of object can be altered. For example: Output: p1 object contain data : Product(item=laptop, price=25000) p2 copied object contains default data of p1: Product(item=laptop, price=25000) p3 contain altered data of p1 : Product(item=laptop, price=20000) Default and named arguments in data classWe can also assign the default arguments in primary constructor of data class. These default values can be changed later on program if required. For example: Output: Product(item=laptop, price=20000)
Next TopicKotlin Sealed Class
|