Skip to content

Latest commit

 

History

History
24 lines (18 loc) · 515 Bytes

safe.md

File metadata and controls

24 lines (18 loc) · 515 Bytes

Safe

Get rid of those pesky NullPointerExceptions, you know, The Billion Dollar Mistake

var output: String
output = null   // Compilation error

Kotlin protects you from mistakenly operating on nullable types

val name: String? = null    // Nullable type
println(name.length())      // Compilation error

And if you check a type is right, the compiler will auto-cast it for you

fun calculateTotal(obj: Any) {
    if (obj is Invoice)
        obj.calculateTotal()
}