AI Generate Kotlin docs instantly

Kotlin Cheat Sheet

Quick reference guide with copy-paste ready code snippets

Try DocuWriter Free

Getting Started

5 snippets

Your first steps with Kotlin basics

Hello World

fun main() {
    println("Hello, World!")
}

Comments

// Single line comment
/* Multi-line
   comment */
/** KDoc comment */

User Input

print("Name: ")
val name = readLine()

Type Conversion

"42".toInt()      // String to Int
42.toString()     // Int to String
"3.14".toDouble() // to Double

Check Type

variable is String    // true/false
variable::class.simpleName

Variables & Data Types

7 snippets

Declaring and using different data types

Val (Immutable)

val name = "John"

Var (Mutable)

var count = 0

Type Annotation

val age: Int = 25

Nullable

var name: String? = null

List

val fruits = listOf("apple", "banana")

Mutable List

val nums = mutableListOf(1, 2, 3)

Map

val ages = mapOf("John" to 30, "Jane" to 25)

Operators

6 snippets

Symbols for calculations and comparisons

Arithmetic

5 + 3   // 8 (addition)
5 - 3   // 2 (subtraction)
5 * 3   // 15 (multiplication)
5 / 3   // 1 (integer division)
5.0 / 3 // 1.666... (double division)
5 % 3   // 2 (modulus)

Comparison

5 == 5  // true (structural equality)
5 === 5 // true (referential equality)
5 != 3  // true (not equal)
5 > 3   // true (greater)
5.compareTo(3) // 1 (compare)

Logical

true && false   // false (AND)
true || false   // true (OR)
!true           // false (NOT)

Null Safety

name?.length     // null if name is null
name ?: "default" // Elvis operator
name!!           // assert non-null

Range

1..5      // 1, 2, 3, 4, 5 (inclusive)
1 until 5 // 1, 2, 3, 4 (exclusive)
5 downTo 1 // 5, 4, 3, 2, 1
1..10 step 2 // 1, 3, 5, 7, 9

In/Contains

5 in 1..10     // true
"a" in listOf("a", "b") // true
5 !in 1..3     // true

Tired of looking up syntax?

DocuWriter.ai generates documentation and explains code using AI.

Try Free

Loops

9 snippets

Repeating code blocks efficiently

For Range

for (i in 1..5) {
    println(i)  // 1, 2, 3, 4, 5
}

For Until

for (i in 0 until 5) {
    println(i)  // 0, 1, 2, 3, 4
}

For DownTo

for (i in 5 downTo 1) {
    println(i)  // 5, 4, 3, 2, 1
}

For Step

for (i in 0..10 step 2) {
    println(i)  // 0, 2, 4, 6, 8, 10
}

ForEach

list.forEach { println(it) }
list.forEachIndexed { i, item -> println("$i: $item") }

While

while (count < 10) {
    count++
}

Do While

do {
    count++
} while (count < 10)

Break/Continue

for (i in 1..10) {
    if (i == 5) continue
    if (i == 8) break
    println(i)
}

Repeat

repeat(5) { index ->
    println("Index: $index")
}

Conditionals

4 snippets

Making decisions in your code

If Expression

val max = if (a > b) a else b

When

when (x) {
    1 -> println("one")
    2, 3 -> println("two or three")
    in 4..10 -> println("4 to 10")
    else -> println("other")
}

When Expression

val result = when {
    x > 0 -> "positive"
    x < 0 -> "negative"
    else -> "zero"
}

If Block

if (x > 0) {
    println("positive")
} else if (x < 0) {
    println("negative")
} else {
    println("zero")
}

Functions

5 snippets

Function

fun greet(name: String): String {
    return "Hello, $name!"
}

Expression Body

fun add(a: Int, b: Int) = a + b

Default Param

fun greet(name: String = "World") = "Hello, $name!"

Lambda

val sum = { a: Int, b: Int -> a + b }

Extension

fun String.addExclaim() = this + "!"

Null Safety

4 snippets

Safe Call

val len = name?.length

Elvis

val len = name?.length ?: 0

Not-Null Assert

val len = name!!.length

Let

name?.let { println(it) }

Classes & OOP

5 snippets

Class

class Dog(val name: String) {
    fun bark() = "$name says woof!"
}

Data Class

data class Person(val name: String, val age: Int)

Object

object Singleton {
    fun doSomething() { }
}

Companion

class MyClass {
    companion object {
        fun create() = MyClass()
    }
}

Sealed Class

sealed class Result {
    data class Success(val data: String) : Result()
    data class Error(val msg: String) : Result()
}

Collection Operations

6 snippets

Map

list.map { it * 2 }

Filter

list.filter { it > 0 }

Reduce

list.reduce { acc, n -> acc + n }

Find

list.find { it > 10 }

GroupBy

people.groupBy { it.city }

SortedBy

people.sortedBy { it.age }

Coroutines

3 snippets

Launch

launch {
    delay(1000)
    println("Done")
}

Async

val result = async { fetchData() }
val data = result.await()

Suspend Fun

suspend fun fetchData(): String {
    delay(1000)
    return "data"
}

More Cheat Sheets

FAQ

Frequently asked questions

What is a Kotlin cheat sheet?

A Kotlin cheat sheet is a quick reference guide containing the most commonly used syntax, functions, and patterns in Kotlin. It helps developers quickly look up syntax without searching through documentation.

How do I learn Kotlin quickly?

Start with the basics: variables, control flow, and functions. Use this cheat sheet as a reference while practicing. For faster learning, try DocuWriter.ai to automatically explain code and generate documentation as you learn.

What are the most important Kotlin concepts?

Key Kotlin concepts include variables and data types, control flow (if/else, loops), functions, error handling, and working with data structures like arrays and objects/dictionaries.

How can I document my Kotlin code?

Use inline comments for complex logic, docstrings for functions and classes, and README files for projects. DocuWriter.ai can automatically generate professional documentation from your Kotlin code using AI.

Code Conversion Tools

Convert Kotlin to Other Languages

Easily translate your Kotlin code to other programming languages with our AI-powered converters

Related resources

Stop memorizing. Start shipping.

Generate Kotlin Docs with AI

DocuWriter.ai automatically generates comments, docstrings, and README files for your code.

Auto-generate comments
Create README files
Explain complex code
API documentation
Start Free - No Credit Card

Join 33,700+ developers saving hours every week