The Kotlin Elvis operator can be used in conjunction with nullable types to define a default value that is to be returned in the event that a value or expression result is null. The Elvis operator (?:) is used to separate two expressions. If the expression on the left does not resolve to a null value that value is returned, otherwise the result of the rightmost expression is returned. This can be thought of as a quick alternative to writing an if-else statement to check for a null value. Consider the following code:
if (myString != null) {
return myString
} else {
return "String is null"
}
The same result can be achieved with less coding using the Elvis operator as follows:
return myString ?: "String is null"