Kotlin Cheat Sheet
Tags: kotlin, cheatsheet
Published: 2021-05-13 09:55:09


### ComponentN Functions

```kotlin
class Person(val name: String, val age: Int) {
	operator fun component1(): String {
		return name
	}

	operator fun component2(): Int {
		return age
	}
}
```

References
----------

* [Defining Variables](https://kotlinlang.org/docs/reference/basic-syntax.html#defining-variables) _(kotlinlang.org)_
* [Strings Documentation](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html) _(kotlinlang.org)_
* [String Templates](https://kotlinlang.org/docs/reference/basic-types.html#string-templates) _(kotlinlang.org)_
* [Basic Types](https://kotlinlang.org/docs/reference/basic-types.html) _(kotlinlang.org)_
* [Companion Objects](https://kotlinlang.org/docs/reference/object-declarations.html#companion-objects) _(kotlinlang.org)_
* [Null Safety](https://kotlinlang.org/docs/reference/null-safety.html) _(kotlinlang.org)_
* [Collections Overview](https://kotlinlang.org/docs/reference/collections.html) _(kotlinlang.org)_
* [Collections Documentation](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/index.html) _(kotlinlang.org)_
* [Functions Documentation](https://kotlinlang.org/docs/reference/functions.html) _(kotlinlang.org)_
* [Classes Documentation](https://kotlinlang.org/docs/reference/classes.html) _(kotlinlang.org)_
* [Destructuring Declarations](https://kotlinlang.org/docs/reference/multi-declarations.html) _(kotlinlang.org)_
### Objects & Lists

```kotlin
val person = Person("Adam", 100)
val (name, age) = person

val pair = Pair(1, 2)
val (first, second) = pair

val coordinates = arrayOf(1, 2, 3)
val (x, y, z) = coordinates
```

### While Loops

```kotlin
while (x > 0) {
    x--
}

do {
    x--
} while (x > 0)
```

Destructuring Declarations
--------------------------
{: .-two-column}

### When Statements

```kotlin
when (direction) {
    NORTH -> {
        print("North")
    }
    SOUTH -> print("South")
    EAST, WEST -> print("East or West")
    "N/A" -> print("Unavailable")
    else -> print("Invalid Direction")
}
```

### For Loops

```kotlin
for (i in 0..10) { } // 1 - 10
for (i in 0 until 10) // 1 - 9
(0..10).forEach { }
for (i in 0 until 10 step 2) // 0, 2, 4, 6, 8
```

### If Statements

```kotlin
if (someBoolean) {
    doThing()
} else {
    doOtherThing()
}
```

### Inheritance & Implementation

```kotlin
open class Vehicle
class Car : Vehicle()

interface Runner {
    fun run()
}

class Machine : Runner {
    override fun run() {
        // ...
    }
}
```

Control Flow
------------

### Secondary Constructors

```kotlin
class Person(val name: String) {
    private var age: Int? = null

    constructor(name: String, age: Int) : this(name) {
        this.age = age
    }
}

// Above can be replaced with default params
class Person(val name: String, val age: Int? = null)
```

### Primary Constructor

```kotlin
class Person(val name: String, val age: Int)
val adam = Person("Adam", 100)
```

### Static Functions

```kotlin
class Fragment(val args: Bundle) {
    companion object {
        fun newInstance(args: Bundle): Fragment {
            return Fragment(args)
        }
    }
}

val fragment = Fragment.newInstance(args)
```

* [Companion Objects](https://kotlinlang.org/docs/reference/object-declarations.html#companion-objects)

Classes
-------

### Reference
* [Kotlin Cheat Sheet](https://cheatsheetmaker.com/kotlin) - [Cheat Sheet Maker](https://cheatsheetmaker.com)