0Pricing
Android Academy · Lesson

Safe Calls and Elvis

?. and ?: operators.

Safe Calls and Elvis is a free Android Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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)
}

Safe Call on Null

When the receiver is null, the safe call short-circuits and returns null. No exception is thrown.

Here the printed result is the word null, not a crash.

fun main() {
    val name: String? = null
    println(name?.length)  // prints: null
}

Chaining Safe Calls

You can chain ?. across several steps. If any link is null, the whole chain stops and yields null.

This is perfect for navigating nested optional data.

class Address(val city: String?)
class User(val address: Address?)

fun cityOf(u: User?): String? {
    return u?.address?.city
}

The Elvis Operator

The Elvis operator ?: supplies a fallback when the left side is null. It is named after the singer's hairstyle, which the symbol resembles.

Read it as: use the left value, or else use the right value.

fun main() {
    val name: String? = null
    val shown = name ?: "Guest"
    println(shown)  // prints: Guest
}

Elvis with Safe Calls

Safe call and Elvis work beautifully together. Use ?. to reach into a value and ?: to provide a default if anything was null.

This pattern turns a nullable result into a guaranteed non-null one.

fun main() {
    val name: String? = null
    val len: Int = name?.length ?: 0
    println(len)  // prints: 0
}

Default Length Example

Here the result type is a plain Int, not Int?. The Elvis operator removed the nullability.

After ?: with a non-null fallback, the compiler knows the value can never be null.

val title: String? = "Kotlin"
val length: Int = title?.length ?: 0
// length is a non-null Int

Elvis for Early Return

Because return and throw are expressions in Kotlin, you can put them on the right of Elvis.

This is a clean way to bail out early when a required value is missing.

fun process(input: String?) {
    val text = input ?: return
    println(text.uppercase())
}

Safe Call with let

Combine ?. with let to run a block only when the value is not null. Inside, the value is non-null and named it.

This is a common idiom for acting on optional values.

fun main() {
    val email: String? = "a@b.com"
    email?.let {
        println("Sending to " + it)
    }
}

Putting It Together

A short program can show safe call and Elvis cooperating to produce a friendly message.

Notice how no if statement is needed, yet the code never risks a null crash.

fun main() {
    val nickname: String? = null
    val display = nickname?.uppercase() ?: "ANONYMOUS"
    println(display)
}

When to Use Which

Use ?. when you want a null result to flow through harmlessly. Use ?: when you want to replace null with a concrete default.

Together they cover the vast majority of everyday null handling without verbose checks.

val count = list?.size ?: 0
// safe call reads size, Elvis defaults to 0

Quick Check

Test your grasp of safe calls and the Elvis operator.

Recap

The safe call ?. calls a member only if the receiver is not null, otherwise yielding null. The Elvis operator ?: replaces a null with a fallback value.

Chaining them removes nullability cleanly. Next you will explore scope functions like let and also in more depth.

Frequently asked questions

Is the “Safe Calls and Elvis” lesson free?

Yes — the full text of “Safe Calls and Elvis” is free to read here on the web, and the Android Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Android Academy course, upgrade to CoddyKit PRO.

What will I learn in “Safe Calls and Elvis”?

?. and ?: operators. You practise Android Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Android Academy?

No prior experience is required. Android Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Safe Calls and Elvis” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Android Academy lesson?

Yes. Every Android Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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