Back
Close

Kotlin Tour

joow
10.6K views

Classes

Introduction

https://kotlinlang.org/docs/reference/classes.html#classes

The keyword class is used to declare a class :

class Empty

Constructors

https://kotlinlang.org/docs/reference/classes.html#constructors

Classes can have one primary constructor and multiple secondary constructors.

Property

https://kotlinlang.org/docs/reference/properties.html

There is no such thing as a field in a Kotlin class. Instead classes may have properties.

Data classes

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

Using the magic data keyword we can transform a class in a data class having the following code handled for us :

- `equals()`/`hashCode()`
- `toString()`
- `componentN()` functions for each declared property
- `copy()` function

Pair and Triple

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

Pair and Triple classes are data classes allowing to return a pair or a triple of elements.

Destructuring

https://kotlinlang.org/docs/reference/multi-declarations.html

Data classes can be destructured :

val (country, city) = Pair("France", "Bordeaux")
val (_, city) = Pair("France", "Bordeaux")       // don't care about country

Equality

https://kotlinlang.org/docs/reference/equality.html

There are two equality operators in Kotlin :

  1. == (and !=) as structural equality (or functional equality)
  2. === (and !==) as referential equality
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants