Chapter 7. data classes: Dealing with Data
Nobody wants to spend their life reinventing the wheel.
Most applications include classes whose main purpose is to store data, so to make your coding life easier, the Kotlin developers came up with the concept of a data class. Here, you’ll learn how data classes enable you to write code that’s cleaner and more concise than you ever dreamed was possible. You’ll explore the data class utility functions, and discover how to destructure a data object into its component parts. Along the way, you’ll find out how default parameter values can make your code more flexible, and we’ll introduce you to Any, the mother of all superclasses.
== calls a function named equals
As you already know, you can use the
== operator to check for equality. Behind the scenes, each time you use the
== operator, it calls a function named
equals. Every object has an
equals function, and the implementation of this function determines how the
== operator will behave.
By default, the
equals function checks for equality by checking whether two variables hold references to the same underlying object.
To see how this works, suppose that we have two
Wolf variables named
w2 hold references to the same
Wolf object, comparing them with the
== operator will evaluate to
w2 hold references to separate
Wolf objects, comparing ...