Kotlin Quick Reference Sheet
Kotlin Quick Reference Sheet
This reference summarizes the topics covered in the Kotlin Bootcamp course in the form of code
snippets. See the Kotlin Language Documentation for full reference. See the Kotlin Koans for more
snippets to practice with. See the Kotlin Bootcamp course if you need anything explained.
Lesson 0
Lesson 1
Hello Kotlin function
Hello Kotlin program
Operators
Type conversion
Number formatting
val (immutable) & var (mutable)
Nullability
Strings / String Templates
if/else
When
listOf / mutableListOf
arrayOf / mutableArrayOf / intArray…
for loop
for (element in swarm) {...}
Lesson 2
Functions
Compact Functions
Filters
Lambas (anonymous functions)
Higher order functions (fun with fun arg)
Lesson 3
Class
Visibility
Inheritance
Abstract classes
Interfaces
Data Classes
Composition
Singleton / object
enum
Lesson 4
Pairs
Lists
Mapping
Constants
Extension functions
Property extensions
Generic classes
Generics: Full example
Generic constraint
In and Out Types
Generic functions and methods
Inline / reified
Annotations
Reflection
Annotations for getters and setters
Labeled breaks
Lesson 5
Lambda recap
Higher order function
Standard Library: apply & run
Standard Library: with & repeat
Inline
Lambda instead ofSAM
Lesson 0
Lesson 1
printHello()
var rocks: Int = null //Error "hello" + "fish" + "!"
var marbles: Int? = null "I have $numberOfFish fish"
fishFoodTreats?.dec() "Print ${ numberOfFish + 5 } fish"
"fish" == "fish"
var l otsOfFish: List<String?> = val message = "You are ${ if (length <
l istOf(null, null) 5) "fried" else "safe" } fish"
if/else When
val isHot =
if (temperature > 90) true else false
for loop
Lesson 2
fun fishFood (hour: Int, day: String = fun shouldChangeWater (day: String,
"Tuesday"): String {} temperature: Int = 22, dirty: Int =
20): Boolean {
fun isTooHot(temperature: Int): Boolean return when {
= temperature > 30 isTooHot(temperature)-> true
else -> false
}
}
{ println("Hello") }()
println( decorations.filter {it[0] ==
'p'}) val waterFilter = { dirty: I
nt - > dirty
/ 2 }
val waterFilter : (Int) -> I nt = {
dirty -> dirty / 2 }
Lesson 3
Class Visibility
i nit { class:
// do stuff sealed - only subclass in same file
}
inside class:
val volume: I nt public - default. Everywhere.
get() { private - inside class, not subclasses
return w * h * l / 1000 protected - inside class and subclasses
}
internal - module
i nit {
// do stuff with volume
}
}
Interfaces Composition
Lesson 4
Pairs Lists
Mapping Constants
fun AquariumPlant?.pull() {
this?.apply {
println("removing $this")
}
}
fun genericsExample() {
val aquarium = Aquarium(TapWater())
aquarium.waterSupply.addChemicalCleanes
()
}
Annotations Reflection
println(a.annotationClass.simpleName)
}
Annotations for getters and setters Labeled breaks
@ set:OnSet
v ar needsFood: boolean = false
}
Lesson 5
Standard Library: apply & run Standard Library: with & repeat
fish.run { with(fish.name) {
name println(name)
} }
val f ish2 =
F ish().apply { repeat(3) { rep ->
name = “ sharky” println(" current repetition:
} $rep")}