Left Index

Elvis Operator

The Elvis operator in Kotlin is an operator that receives two inputs and returns the first argument if it is non-null or the second one otherwise. It is fancily called the null-coalescing operator. It is a variant of the ternary operator but for null-safety checking.

As you may know, supports nullability explicitly. This is an attempt to minimize the dreaded NullPointerException during runtime and catch them all at compile time

Elvis operator (?:) is used to return the not null value even the conditional expression is null.

package com.theitbulls.kotlinex

fun main(args: Array<String>) {
	var msg: String? = null
	println(msg?: "This is elvis demo")
This is elvis demo