ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Follow publication

Safe Call and Elvis Operator usage in Kotlin

This article is for those who have a basic understanding of ‘ Nullability ‘. The only reason I am writing this article is because I once faced lots of confusion in their syntax and usage and that’s why I will try to make it understandable in my way. I will specifically talk about two ways of Null checks in Kotlin and that is ‘ Safe Call Operator ‘ and ‘ Elvis Operator ‘.

So let’s start with understanding what is ‘ Safe Call ‘ and how it is used for null checks.

A safe call is denoted by the operator( ? ) — It is used in case you want to check the null condition and if the expression is null then by default it will return null or else it will return the value that is instructed. Let’s understand by example how it’s done.

Safe Call Operator usage. ( Pic 1 )

What’s happening in the above program? A variable of type String is declared but wait look at it carefully. That’s not simply String, that is of type nullable String( String? ). It means ‘ name ‘ String can be null. It will tell the compiler to check to do nullability check on ‘ name ‘ String by using ‘ ? ‘ operator also known as Safe Call Operator. Guess the output!

Pic 2. Output using Safe Call Operator

As you can see output is ‘ null ‘. That’s the benefit of using this operator. If the expression has a null value then it will simply return null without any further manipulation and if the expression isn’t null then it will return the result of operations performed on the variable.

Pic 3 — Example for Safe Call Operator

The above program is taken from one of my personal projects. Here you can see that the variable ‘ exerciseTimer ‘ is of type nullable CountDownTimer. If it’s nullable then a check will be done and in this case, I used Safe Call Operator to perform a null check. If it’s not null then exerciseTimer?.cancel() would have returned the result after performing cancel() method on ‘ exerciseTimer ‘ but again in this case variable is null then you can simply guess the returned result will also be ‘ null ‘.

What will happen if you don’t check for null conditions on the nullable type declaration? Let me show you first.

Pic 4 — Program for not applying null check on nullable type declaration

As you can see warning with a red curly line. Let’s see what it returns on running the program.

Pic 5 — Error on not applying null check on nullable type declaration

It will throw an error or it will simply tell you to check if the value is not null. So you have to be careful when having nullable type declaration.

Elvis Operator ( ?: ) — Now let’s understand Elvis Operator what is it and when it is used.

You have seen a different kind of sign here ‘ ?: ‘ here. In Kotlin this operator is known as Elvis Operator. First, let’s see one program in action:

Pic 6 — Program using Elvis Operator.

You must be giving a thought to what’s different in it than Safe Call Operator. To understand the difference firstly you need to see the result of the above program.

Pic 7 — Result of the above program using Elvis Operator.

As you have noticed result printed is 0. In the case of Safe Operator Call it would have printed ‘ null ‘. And that’s the difference between Safe Operator Call & Elvis Operator.

Safe Operator Call will return ‘ null ‘ by default if the value is null but using Elvis Operator in conjunction with Safe Call Operator you can ask for the desired output other than ‘ null ‘ if the assigned value is null. Instruction right to ‘ ?: ‘ operator will be returned if null is found in nullability check.

In this case, the value right to ‘ ?: ‘ operator is 0. That’s why it printed 0 not null.

So Elvis Operator gives you the independence to do more in case express is found null during the check.

So, there is a lot more we can do using these operators. We can use it on any type of nullable declaration whether it’s String, Array, List, or whatever. I have kept the focus on basic manipulations and tried to clear out the confusion while using these two.

Thanks a lot!

You can connect with me on Twitter — https://twitter.com/lazy_doer

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Published in ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Written by Abhas Kumar

Personal, Food, Fitness, Travel and Tech ( Javascript & Kotlin )

No responses yet

Write a response