Android ● ○ ○ Kotlin 2.3

Kodebits Day 3: Null Safety

Apr 10 2026
Kotlin’s null safety features help avoid NullPointerExceptions.

What is the output?

fun main() {
  var b: String? = "abc"
  b = null
  val l = b?.length ?: -1
  println(l)

  b = "kotlin"
  println(b?.length)
}


Try it in the online Kotlin Playground →

[spoiler title="Solution"]

Answer:

-1
6

Explanation:

The Elvis operator ?: returns the right side if the left is null. The first print, b is null, the second it has a value.

[/spoiler]


Further Reading