Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a normal class and a data class in Kotlin?

Tags:

kotlin

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)) } 
like image 293
Trần Đức Tâm Avatar asked May 26 '17 04:05

Trần Đức Tâm


People also ask

When should I use data class Kotlin?

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.

What is a benefit to using data classes in Kotlin?

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.

What is the advantage of data class?

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.

What is a data class?

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.


1 Answers

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.

like image 153
chandil03 Avatar answered Sep 20 '22 01:09

chandil03