Getting Started
5 snippetsYour 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 DoubleCheck Type
variable is String // true/false
variable::class.simpleNameVariables & Data Types
7 snippetsDeclaring and using different data types
Val (Immutable)
val name = "John"Var (Mutable)
var count = 0Type Annotation
val age: Int = 25Nullable
var name: String? = nullList
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 snippetsSymbols 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-nullRange
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, 9In/Contains
5 in 1..10 // true
"a" in listOf("a", "b") // true
5 !in 1..3 // trueTired of looking up syntax?
DocuWriter.ai generates documentation and explains code using AI.
Loops
9 snippetsRepeating 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 snippetsMaking decisions in your code
If Expression
val max = if (a > b) a else bWhen
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 snippetsFunction
fun greet(name: String): String {
return "Hello, $name!"
}Expression Body
fun add(a: Int, b: Int) = a + bDefault 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 snippetsSafe Call
val len = name?.lengthElvis
val len = name?.length ?: 0Not-Null Assert
val len = name!!.lengthLet
name?.let { println(it) }Classes & OOP
5 snippetsClass
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 snippetsMap
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 snippetsLaunch
launch {
delay(1000)
println("Done")
}Async
val result = async { fetchData() }
val data = result.await()Suspend Fun
suspend fun fetchData(): String {
delay(1000)
return "data"
}