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
fun printHello () {
println ("Hello Kotlin")
}
printHello()
Hello Kotlin program
*, fish.times(6)
/, fish.div(10)
+, fish.plus(3)
-, fish.minus(3)
Type conversion
1.toLong()
1.toString()
Number formatting
Nullability
val isHot =
if (temperature > 90) true else false
When
when (numberOfFish) {
0 -> println("Empty tank")
in 1..50 -> println("Got fish!")
else -> println("Perfect!")
}
listOf / mutableListOf
val myList =
mutableListOf("tuna",,"shark")
myList.remove("shark") // OK!
val school =
arrayOf("tuna","salmon","shark")
val mix = arrayOf("fish", 2)
println(Arrays.toString(intArrayOf(2, "foo")))
Lesson 2
Functions
fun shouldChangeWater (day: String, temperature: Int = 22, dirty: Int = 20):
Boolean {
return when {
isTooHot(temperature)-> true
else -> false
}
}
{ println("Hello") }()
Class
class Aquarium(var length: Int = 100, var width: Int = 20, var height: Int = 40) {
init {
// do stuff
}
init {
// do stuff with volume
}
}
Visibility
package:
public - default. Everywhere
private - file
internal - module
class:
sealed - only subclass in same file
inside class:
public - default. Everywhere.
private - inside class, not subclasses
protected - inside class and subclasses
internal - module
Inheritance
Interfaces
interface FishAction {
fun eat()
}
Data Classes
data class Decorations(val rocks: String, val wood: String, val diver: String){
}
Composition
fun delegate() {
val pleco = Plecostomus()
println("Fish has has color ${pleco.color}")
pleco.eat()
}
interface FishAction {
fun eat()
}
interface FishColor {
val color: String
}
Singleton / object
object Database
object MobyDickWhale {
val author = "Herman Melville"
}
enum
Lesson 4
Pairs
Lists
listOf(1, 5, 3).sum()
listOf("a", "b", "cc").sumBy { it.length }
Mapping
println(cures["white spots"])
inventory.put("tank scrubber", 3)
inventory.remove("fish net")
Constants
object Constants {
const val CONSTANT2 = "object constant"
}
class MyClass {
companion object {
const val CONSTANT3 = "constant in companion"
}
}
Extension functions
fun extensionExample() {
“Does it have spaces?”.hasSpaces()
}
fun propertyExample() {
val plant = GreenLeafyPlant(30)
plant.isGreen // true
}
Generic classes
class MyList<T> {
fun get(pos: Int): T {
TODO("implement")
}
fun addItem(item: T) {}
}
fun workWithMyList() {
val intList: MyList<String>
val fishList: MyList<Fish>
}
fun genericsExample() {
val aquarium = Aquarium(TapWater())
aquarium.waterSupply.addChemicalCleanes()
}
Generic constraint
Non-nullable:
class Aquarium<T: Any>(val waterSupply: T)
fun genericsFunExample() {
val aquarium = Aquarium(TapWater())
isWaterClean(aquarium)
}
@file:JvmName(“InteropFish”)
@JvmStatic fun interop()
annotation class ImAPlant
@ImAPlant class Plant{...}
Reflection
val classobj=Plant::class
for(m in classobj.declaredMemberFunctions){
println(m.name)
}
Annotations for getters and setters
@Target(PROPERTY_GETTER)
annotation class OnGet
@Target(PROPERTY_SETTER)
Annotation class OnSet
@set:OnSet
var needsFood: boolean = false
}
Labeled breaks
fun labels() {
loop@ for (i in 1..100) {
for (j in 1..100) {
if (i > 10) break@loop
}
}
}
Lesson 5
Lambda recap
fish.run {
name
}
with(fish.name) {
println(name)
}
fun example() {
runNow {
println(“Passing a lambda as a Runnable”)
}