Elvis Operator (?:) in Kotlin
Definition
- This operator is used for null safety in Kotlin.
- It is mainly used for operations involving boxed values (Values which may hold "null").
- It is represented by "?:"
- If the value to the left of Elvis Operator is null, the value on the right side of the operator is returned as the result.
- If the value to the left sidde of Elvis Operator is not null then the same value is returned as the result.
Syntax
/*If the value at the variable "string" is null, "100" is assigned to the variable "length" and if it is not null, the value of the length of the string is assigned to the variable "length".*/
value length = string?.length ?: 100
Elvis Operator (?:) Example Program in Kotlin
//Elvis Operator Example Program in Kotlin
//Operator Kotlin Programs, Basic Kotlin Program
fun main(args: Array<String>) {
//Declaring a boxed string and assigning a non-null value
var name: String? = "Little Drops"
var result = getLength(name)
println("Length of $name is $result")
//Assigning null to the name variable
name = null
result = getLength(name)
println("Length of $name is $result")
}
//Function to find the length of a boxed string which returns 0 when the string is null.
fun getLength(name : String?) : Int?{
return name?.length ?: 0
}
Sample Output
Length of Little Drops is 12
Length of null is 0
Kotlin Operators
- Kotlin Operators Overview
- Arithmetic Operators (Mathematical Operators) in Kotlin
- Assignment Operators and Augmented Assignment Operators in Kotlin
- Unary Operators (Sign, Inverts, Increment, and Decrement) in Kotlin
- Logical Operators in Kotlin
- Equality Operators (==, !=) and Referential equality Operators (===, !==) in Kotlin
- Comparison Operators in Kotlin
- In Operator (in and !in) in Kotlin
- Is Operator (is and !is) in Kotlin
- Indexed Access Operator [, ] in Kotlin
- Not Null Assertion Operator in Kotlin
- Safe Call Operator (?.) in Kotlin
- Elvis Operator (?:) in Kotlin
- Range Operator (..) in Kotlin
Read More Articles
- Read Data Input using Scanner in Kotlin
- Declare Variables In Kotlin
- Double Data type Usage and Type Conversion in Kotlin
- print and println Data Output in Kotlin
- Arithmetic Operators (Mathematical Operators) in Kotlin
- Unary Operators (Sign, Inverts, Increment, and Decrement) in Kotlin
- Printing Variables and Values in Kotlin
- Float Data type Usage and Type Conversion in Kotlin
- Equality Operators (==, !=) and Referential equality Operators (===, !==) in Kotlin
- Long Data type Usage and Type Conversion in Kotlin
- Byte Data type Usage and Type Conversion in Kotlin
- Comparison Operators in Kotlin
- In Operator (in and !in) in Kotlin
- Assignment Operators and Augmented Assignment Operators in Kotlin
- Is Operator (is and !is) in Kotlin
- Read Data Input from Command Line in Kotlin
- Read String Data Input in Kotlin
- Indexed Access Operator [, ] in Kotlin
- Char Data type Usage and Type Conversion in Kotlin
- Elvis Operator (?:) in Kotlin
- Not Null Assertion(!!) Operator in Kotlin
- Logical Operators in Kotlin
- Safe Call Operator (?.) in Kotlin
- Repeat and Its Usage in Kotlin
- Boolean Data type Usage and Type Conversion in Kotlin