0Pricing
Kotlin Academy · Lesson

Safe Calls and Elvis

?. and ?: operators.

Safe Calls and Elvis is a free Kotlin 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 Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Working with Nullables Cleanly

Writing if (x != null) everywhere gets verbose. Kotlin offers two concise operators for nullable values:

  • the safe call ?.
  • the Elvis operator ?:

The Safe Call Operator

The safe call ?. calls a method or reads a property only if the value is not null. Otherwise the whole expression evaluates to null.

No crash, no explicit check.

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

Safe Call on Null

When the receiver is null, the safe call short-circuits and returns null instead of throwing.

This is the key difference from a plain dot call.

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

Chaining Safe Calls

Safe calls can be chained. If any link in the chain is null, the entire result becomes null.

This avoids deeply nested null checks.

fun main() {
    val text: String? = "hello world"
    val result = text?.uppercase()?.substring(0, 5)
    println(result)
}

The Elvis Operator

The Elvis operator ?: provides a fallback value when the left side is null.

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

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

Elvis with a Real Value

If the left side is not null, the Elvis operator simply returns it and ignores the fallback.

The fallback is only used when the value is missing.

fun main() {
    val name: String? = "Sam"
    val display = name ?: "Guest"
    println(display)
}

Combining Safe Call and Elvis

These operators are often used together. A safe call may produce null, and Elvis supplies a default.

This is one of the most common Kotlin idioms.

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

Elvis for Early Return

The right side of Elvis can be return or throw. This is a clean way to exit early when a value is missing.

If id is null, the function returns immediately.

fun process(id: String?) {
    val safeId = id ?: return
    println("Processing " + safeId)
}

fun main() {
    process("A-100")
    process(null)
    println("done")
}

Safe Call with let

Combine ?. with let to run a block only when the value is not null.

Inside the block, it refers to the non-null value.

fun main() {
    val name: String? = "Kotlin"
    name?.let {
        println("Length is " + it.length)
    }
}

Default Numbers with Elvis

Elvis is handy for converting nullable numbers into safe defaults before arithmetic.

Here a null count becomes 0 so the math always works.

fun main() {
    val count: Int? = null
    val total = (count ?: 0) + 5
    println(total)
}

Putting It Together

Together these operators replace bulky null checks:

  • ?. calls only when not null
  • ?: supplies a fallback when null
  • Chaining and let make complex flows concise
fun main() {
    val input: String? = null
    val name = input?.trim()?.uppercase() ?: "UNKNOWN"
    println(name)
}

Quick Check

Test your understanding of safe calls and Elvis.

Recap

You learned the two essential nullable operators:

  • Safe call ?. returns null instead of crashing
  • Elvis ?: provides a fallback value
  • They combine cleanly with let and early returns

Next you will see the !! operator and why it should be used with care.

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 Kotlin 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 Kotlin Academy course, upgrade to CoddyKit PRO.

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

?. and ?: operators. You practise Kotlin 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 Kotlin Academy?

No prior experience is required. Kotlin 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 Kotlin Academy lesson?

Yes. Every Kotlin 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 and Non-Null Types
  2. Safe Calls and Elvis
  3. The !! Operator
  4. Platform Types
← Back to Kotlin Academy