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
- Declare Variables In Kotlin
- Read Data Input using Scanner 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
- Equality Operators (==, !=) and Referential equality Operators (===, !==) in Kotlin
- Printing Variables and Values in Kotlin
- Float Data type Usage and Type Conversion in Kotlin
- Long Data type Usage and Type Conversion in Kotlin
- Comparison Operators in Kotlin
- Byte Data type Usage and Type Conversion in Kotlin
- Is Operator (is and !is) in Kotlin
- In Operator (in and !in) in Kotlin
- Char Data type Usage and Type Conversion in Kotlin
- Read Data Input from Command Line in Kotlin
- Assignment Operators and Augmented Assignment Operators in Kotlin
- Indexed Access Operator [, ] in Kotlin
- Read String Data Input in Kotlin
- Short Data type Usage and Type Conversion in Kotlin
- Boolean Data type Usage and Type Conversion in Kotlin
- Logical Operators in Kotlin
- Elvis Operator (?:) in Kotlin
- Repeat and Its Usage in Kotlin
- Safe Call Operator (?.) in Kotlin