Kotlin Academy · Lesson

Safe Calls and Elvis

?. and ?: operators.

Lesson 2 of 413 steps

Safe Calls and Elvis is a free Kotlin Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, and your progress syncs across the web and the CoddyKit app. The Kotlin Academy course includes 4 lessons in total.

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.

Free to start

Learn Kotlin with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

Is the “Safe Calls and Elvis” lesson free?

Yes — any 3 lessons of the Kotlin Academy learning path, including “Safe Calls and Elvis”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Kotlin Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

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