- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What does "?:" do in Kotlin? (Elvis Operator)
Elvis operator is very common in many programming languages. This is a binary expression that returns the first operand when the expression value is True and it returns the second operand when the expression value is False. Generally, the Elvis operator is denoted using "?:", the syntax looks like −
First operand ?: Second operand
Example
The following example demonstrates how you can use the Elvis operator in Kotlin.
fun main(args: Array<String>) { val x: String? = null val y: String = x ?: "TutorialsPoint.com" // it will check whether the value of x // is NULL or not. If NULL, then // it will return y, otherwise x println(x ?: y) }
Output
In the above example, the Elvis operator will check whether the value of "x" is NULL. If yes, then it will return the first operand "x", otherwise it will return "y". As per our example, it will return "y", as the value of "x" is NULL.
TutorialsPoint.com
- Related Articles
- What does the "===" operator do in Kotlin?
- What Does the // Operator Do?
- What does the &= operator do in Python?
- What does the >> operator do in Python?
- What does 'in' operator do in Python?
- What does 'by' keyword do in Kotlin?
- What does 'is' operator do in Python?
- What does colon ':' operator do in Python?
- What does 'not in' operator do in Python?
- What does the bitwise left shift operator do in Java?
- What does the bitwise right shift operator do in Java?
- What does 'is not' operator do in Python?
- What does the operator || do in a var statement in JavaScript?
- What is the Kotlin double-bang (!!) operator?
- What does "return@" mean in Kotlin?

Advertisements