Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
class is used to declare a class :
Classes can have one primary constructor and multiple secondary constructors.
There is no such thing as a field in a Kotlin class. Instead classes may have properties.
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
Triple classes are data classes allowing to return a pair or a triple of elements.
Data classes can be destructured :
val (country, city) = Pair("France", "Bordeaux") val (_, city) = Pair("France", "Bordeaux") // don't care about country
There are two equality operators in Kotlin :
!=) as structural equality (or functional equality)
!==) as referential equality