Post

Kotlin Cheat sheet

Basic Structure

1
2
3
4
fun main() {
    println("Hello, World!")
}

Variable Declaration

1
2
3
4
5
6
7
8
9
10
// Immutable variable (read-only)
val name: String = "John"

// Mutable variable (modifiable)
var age: Int = 30

// Type inference
val city = "New York"
var isStudent = true

Functions

1
2
3
4
5
6
7
8
9
10
11
12
13
// Function declaration
fun sum(a: Int, b: Int): Int {
    return a + b
}

// Expression body function
fun multiply(a: Int, b: Int) = a * b

// Default arguments
fun greet(name: String = "Guest") {
    println("Hello, $name")
}

Conditional Statements

1
2
3
4
5
6
7
8
9
10
// if-else
val max = if (a > b) a else b

// when
when (x) {
    1 -> println("x is 1")
    2 -> println("x is 2")
    else -> println("x is neither 1 nor 2")
}

Loops

1
2
3
4
5
6
7
8
9
10
11
12
// for loop
for (i in 1..5) {
    println(i)
}

// while loop
var i = 0
while (i < 5) {
    println(i)
    i++
}

Classes and Objects

1
2
3
4
5
6
7
class Person(val name: String, var age: Int)

fun main() {
    val person = Person("John", 30)
    println("Name: ${person.name}, Age: ${person.age}")
}

Inheritance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
open class Animal {
    open fun sound() {
        println("Animal makes a sound")
    }
}

class Dog : Animal() {
    override fun sound() {
        println("Dog barks")
    }
}

fun main() {
    val dog = Dog()
    dog.sound()
}

Data Classes

1
2
3
4
5
6
7
data class User(val name: String, val age: Int)

fun main() {
    val user = User("Alice", 25)
    println(user)
}

Interfaces

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
interface Animal {
    fun sound()
}

class Dog : Animal {
    override fun sound() {
        println("Dog barks")
    }
}

fun main() {
    val dog = Dog()
    dog.sound()
}

Null Safety

1
2
3
4
5
6
7
8
9
10
11
12
13
// Nullable type
var name: String? = "John"
name = null

// Safe call operator
val length = name?.length

// Elvis operator
val length = name?.length ?: 0

// Non-null assertion operator
val length = name!!.length

Collections

1
2
3
4
5
6
7
8
9
10
11
12
13
// List
val fruits = listOf("Apple", "Banana", "Cherry")
println(fruits[0]) // Output: Apple

// MutableList
val vegetables = mutableListOf("Carrot", "Potato")
vegetables.add("Tomato")
println(vegetables)

// Map
val map = mapOf("name" to "John", "age" to 30)
println(map["name"]) // Output: John

Higher-Order Functions and Lambdas

1
2
3
4
5
6
7
8
9
10
11
12
// Higher-order function
fun calculate(a: Int, b: Int, operation: (Int, Int) -> Int): Int {
    return operation(a, b)
}

val sum = calculate(5, 10) { x, y -> x + y }
println(sum) // Output: 15

// Lambda expression
val double = { x: Int -> x * 2 }
println(double(4)) // Output: 8

Extension Functions

1
2
3
4
5
6
fun String.lastChar(): Char = this[this.length - 1]

fun main() {
    println("Kotlin".lastChar()) // Output: n
}

Coroutines

1
2
3
4
5
6
7
8
9
10
import kotlinx.coroutines.*

fun main() = runBlocking {
    launch {
        delay(1000L)
        println("World!")
    }
    println("Hello,")
}

This post is licensed under CC BY 4.0 by the author.