Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Introduction to Kotlin
3.
Elvis Operator in Kotlin
4.
Kotlin Program Without Using Elvis Operator
4.1.
Code
4.2.
Output
4.3.
Explanation
5.
Kotlin Program Using the Elvis Operator
5.1.
Code
5.2.
Output 
5.3.
Explanation
6.
Frequently Asked Questions
6.1.
What is Kotlin?
6.2.
What does the null value mean?
6.3.
Is Kotlin good for App Development?
6.4.
Which is better, Kotlin or Java?
6.5.
In which fields is Kotlin used?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

Elvis Operator in Kotlin

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.

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!!

Live masterclass