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 w1 and w2. If w1 and w2 hold references to the same Wolf object, comparing them with the == operator will evaluate to true:

If, however, w1 and w2 hold references to separate Wolf objects, comparing ...

Get Head First Kotlin now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.