0Pricing
Android Academy · Lesson

Safe Calls and Elvis

?. and ?: operators.

Beyond if Checks

Checking every nullable value with an if works, but it gets verbose fast. Kotlin offers compact operators built for null handling.

This lesson covers the safe call operator ?. and the Elvis operator ?:, two tools you will use constantly.

The Safe Call Operator

The ?. operator calls a method only if the value is not null. If it is null, the whole expression evaluates to null instead of crashing.

It is a safe shortcut for the if-not-null pattern.

fun main() {
    val name: String? = "Coddy"
    println(name?.length)
}

All lessons in this course

  1. Nullable vs Non-Null Types
  2. Safe Calls and Elvis
  3. let, also, and Scope Functions
  4. Avoiding NullPointerExceptions
← Back to Android Academy