Purpose of safe call (`?.`) and Elvis (`?:`) operators

This tests your grasp of Kotlin's idiomatic null safety. Explain the safe call (`?.`) for chaining on nullables and the Elvis operator (`?:`) for providing defaults. Combine them in a one-line example. A multi-line `if/else` is a red flag.
This question tests your grasp of Kotlin's idiomatic null safety features beyond basic `if/else` checks. A strong answer first defines the safe call (`?.`) for chaining on nullable types, then the Elvis operator (`?:`) for providing a default value if the preceding expression is null. Finally, you should provide a single-line code example combining both to demonstrate concise, safe property access. A major red flag is resorting to a verbose `if (x != null)` block, which misses the point of the operators.
Read the original → kotlinlang.org
- #kotlin
- #android
- #null safety
- #operators
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.