I tried to resolve task #6 (DataClass) at Kotlin Koans. When I used the normal class in code, the test case failed.
Here's my code of the data class:
data class Person(val name: String, val age: Int) fun task6(): List<Person> { return listOf(Person("Alice", 29), Person("Bob", 31)) }
Here's result of the data class:
[Person(name=Alice, age=29), Person(name=Bob, age=31)]
Here's my code of the normal class:
class Person(val name: String, val age: Int) fun task6(): List<Person> { return listOf(Person("Alice", 29), Person("Bob", 31)) }
Here's result of the normal class:
[i_introduction._6_Data_Classes.Person@4f47d241, i_introduction._6_Data_Classes.Person@4c3e4790]
Does that mean there is difference between a normal class and a data class in Kotlin. If yes, what is that?
Updated:
Thank @Mallow, you are right. That works:
class Person(val name: String, val age: Int) { override fun toString(): String { return "Person(name=$name, age=$age)" } } fun task6(): List<Person> { return listOf(Person("Alice", 29), Person("Bob", 31)) }
Answer: Kotlin provides a special type of class called data class, which is usually used for objects that act as a store for data properties and has no business logic or member functions. It provides a lot of advantages with reduced boilerplate code.
Data classes as immutable objects gives us one more automatically-generated method that allows us to copy an instance of your class where one or more values of properties changes.
The dataclass decorator, @dataclass, can be used to add special methods to user-defined classes. The advantage to this is that this is done automatically, which means that you can focus a bit more on the functions in your class instead of focusing on the class itself.
A data class is a list of data set allocation attributes and their values. You cannot assign a data class to an object; however, data class may be used for allocation of a scratch tape to be used to write objects.
Most of the time we developers use class to keep only data in classes. Classes have some methods which needs to be overwritten wrt the data it holds. ex: hashCode()
, equals()
.
Data classes automatically take care of such utilities.
From the official documentation:
We frequently create a class to do nothing but hold data. In such a class some standard functionality is often mechanically derivable from the data. In Kotlin, this is called a data class and is marked as
data
.
The compiler automatically derives the following members from all properties declared in the primary constructor:
- equals()/hashCode() pair,
- toString() of the form "User(name=John, age=42)",
- componentN() functions corresponding to the properties in their order of declaration,
- copy() function (see below). If any of these functions is explicitly defined in the class body or inherited from the base types, it will not be generated.
To read more, check data-classes
About the result, Technically, you are getting is different because of implementation of toString()
method. data class' toString()
method uses data class properties and values to form returning string. General class' toString()
method uses hash code to form returning string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With