What if your app could avoid crashes just by using a tiny symbol?
Why Safe call operator (?.) in Kotlin? - Purpose & Use Cases
Imagine you have a list of friends, and you want to check their phone numbers. But some friends might not have a phone number saved. If you try to get the phone number directly, your program might crash because it found nothing.
Without the safe call operator, you have to write many checks like "if this friend exists, then check if the phone number exists". This makes your code long, hard to read, and easy to forget checks, causing errors or crashes.
The safe call operator (?.) lets you ask for a property or call a function only if the object is not null. If it is null, it just returns null safely without crashing. This makes your code shorter, cleaner, and safer.
if (friend != null) { if (friend.phoneNumber != null) { println(friend.phoneNumber) } }
println(friend?.phoneNumber)
It enables you to write simple, safe code that handles missing or null data gracefully without crashes.
When building a contact app, you can safely show a friend's phone number if it exists, or show nothing if it doesn't, without worrying about errors.
Manual null checks make code long and error-prone.
The safe call operator (?.) simplifies null handling.
It helps avoid crashes by safely accessing nullable data.