Gets rid of Java NullPointerException ▣ Interoperable ▣ Safer code ▣ Gets rid of boilerplate code ▣ Modern programming language ▣ Easier way to develop Android apps 4 Why Kotlin?
2 -> print("x == 2") else -> { print("x is neither 1 nor 2") } } 10 Switch 2.0 - When Multiple cases when (x) { 0, 1 -> print("x == 0 or x == 1") else -> print("otherwise") } Range when (x) { in 1..10 -> print("x is in the range") in validNumbers -> print("x is valid") !in 10..20 -> print("x is outside the range") else -> print("none of the above") }
can have only one of the types from a limited set (restricted hierarchies). Sealed classes cannot have public constructors sealed class Animal{} class Monkey: Animal() class Dog: Animal() Fun animalType (a: Animal) = when(a){ Is Monkey -> print(“Monkey”) Is Dog -> print(“Dog”) }
parameter and returns a function. Lambda reduces boilerplate code Fun sampleFunction(x: String, doStuff: (String)->String){ //higher order function doStuff(x) } //lambda expression val doStuff: (String) -> String = { s -> “Hello $s” } sampleFunction(“Adora”, doStuff)