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

for a data class.

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).

see https://kotlinlang.org/docs/reference/data-classes.html


Most of the time we developers use class to keep only data in classes. Classes have some methods which needs to be overridden 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.


A class represents some data "type" and its behaviour(s) so from that point of view data class isn't any different than a class. But there are certain behaviours and rules about a data class that makes it a bit different:

  1. Calling toString() on a data class dumps a string with all its member properties.
  2. It has componentN method that get member properties by their order n.
  3. It has a copy method which takes the member properties as parameters for making a diff copy of the object.
  4. A data class can not be open. Cant be inherited.
  5. It can not be abstract.
  6. It can not be nested, inner or sealed.
  7. Although it can inherit, define abstract methods and implement interfaces.
  8. data class properties can be destructed into individual variables e.g val (name, address) = Person("name", "address")
  9. Pair(a, b) internally uses data class.

Tags:

Kotlin