-3

Other than the fact that the compiler automatically generates certain functions for Data Class, what is the fundamental difference between them?

Vandit Goel
  • 620
  • 2
  • 9
  • 19
  • 1
    See https://kotlinlang.org/docs/reference/data-classes.html. Why do you think there are differences other than described? – jonrsharpe Jun 13 '20 at 18:43
  • Does this answer your question? [Data classes in Kotlin](https://stackoverflow.com/questions/45069337/data-classes-in-kotlin) – Nicolas Jun 13 '20 at 18:44

1 Answers1

2

In Kotlin, classes declared with the data class keywords simply get some extra methods generated:

  • equals
  • hashcode
  • toString
  • copy
  • componentX

Declaring a regular class and defining these methods manually yields exactly the same thing. There is no other difference at bytecode level.

You do however have some extra limitations (no non-property constructor arguments, limitations on inheritance...), but these are just compile-time limitations so that the generated methods behave in a predictable/non surprising way.

The official doc covers everything in detail about them.

Joffrey
  • 32,348
  • 6
  • 68
  • 100