'Coding has over 700 languages', '67% of programming jobs aren’t in the technology industry', 'Coding is behind almost everything that is powered by electricity'
Last Updated: Jun 30, 2023
Easy

Elvis Operator in Kotlin

gp-icon
Fundamentals of react native
Free guided path
7 chapters
15+ problems
gp-badge
Earn badges and level up

Introduction

Have you ever wondered what the best programming language for android development other than Java is? One such language is Kotlin. It is a programming language similar to Java and was initially developed for JVM (Java Virtual Machine). 

This article will discuss how to use the Elvis operator in Kotlin with some examples.

elvis operator in kotlin

Introduction to Kotlin

Kotlin is a programming language used by android developers. It is used by most developers and is an open-source language developed by JetBrains. It is also considered a Java alternative as it looks like a concise version of it.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Elvis Operator in Kotlin

Elvis operator in Kotlin is a binary operator. It is used in Kotlin to return the operand that is first, given that it is true. It otherwise returns the second operand. Elvis operator is denoted using ‘?:’. 

The syntax for the Elvis operator is

First operand ?: Second operand


You should also know that in Kotlin, it is not necessary for the first operand to be in boolean. It can also result in any object reference. Kotlin will return that reference as long as it is not null. But, the Elvis operator in Kotlin will return the value of the second operand if the first value is null. Kotlin will return an exception if the value of the second operand is also void.

For example- 

// Returns `x` if `x` is not null, return `y` otherwise.
x ?: y 

// Yields `x` if `x` is not null, retuns Code studio
x ?: Code Studio() 
Coding Example

Kotlin Program Without Using Elvis Operator

You can also check for null safety in Kotlin without using the Elvis operator. Let’s look at an example of the same

Code

// Kotlin Program Without Using Elvis Operator

fun main() {
   val name="code studio"
   if (name != null) {
      println(name)
   }
   else{
      println("the value is null")
   }
}

Output

Code Studio

Explanation

The value of the variable ‘name’ in the above program will be checked by the compiler. It will print the value of the variable ‘name’ if it is not null (‘code studio’ in this case), and it will return “the value is null” if the variable is found to be NULL.

We used the ‘if-else’ clause to check for the null safety of this program in Kotlin, like in other languages.

Explanation

Kotlin Program Using the Elvis Operator

It is always better to use the Elvis operator in Kotlin if we want the compiler to return a default value in case a null reference is used. Let's look at a few examples using the Elvis operator in Kotlin

Code

// Kotlin Program Using the Elvis Operator

fun main(args: Array<String>) {
   val x: String? = null
   val y: String = x ?: "Coding Ninjas"
   println(x ?: y)
}

Output 

coding ninjas

Explanation

In the above program. Elvis operator checked if the value of x is null or not. 

Since the value of x was null, it returned y, which was “Coding Ninjas”

Read about Bitwise Operators in C here.

Frequently Asked Questions

What is Kotlin?

Kotlin is a programming language used by android developers. It is used by most developers and is an open-source language developed by JetBrains.

What does the null value mean?

A null value means that the memory location assigned to the reference value is empty, and all the bits are at 0. 

Is Kotlin good for App Development?

Kotlin is among the top choices for app development in today’s times. It is an alternative to java, which is used by app developers extensively.

Which is better, Kotlin or Java?

Kotlin is a more concise version of Java, making some statements easier to write. It provides many more features than Java but takes a bit more time to run.

In which fields is Kotlin used?

Developers can use Kotlin for App Development and web development for various platforms like macOS, IOS, and Android.

Conclusion

This blog discussed and explained how to use the Elvis operator in Kotlin, along with some examples. We learned how to identify a null value in Kotlin with and without using the Elvis Operator.
Look at some of these related articles below:

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Happy Coding!!

Previous article
Kotlin Return and Jump
Next article
Arrays in Kotlin
Guided path
Free
gridgp-icon
Fundamentals of react native
7 chapters
15+ Problems
gp-badge
Earn badges and level up
Live masterclass