For example,
The following expression in Kotlin
val sample1 = sample2 ?: "Undefined"
is equivalent to the following
val sample1 = if(sample2 != null)
sample2
else
"Undefined"
Furthermore, on the right side of the Elvis operator, we may use throw and return expressions, which is particularly handy in functions. As a result, instead of returning a default value on the right side of the Elvis operator, we can throw an exception. For example,
val sample1 = sample2 ?: throw IllegalArgumentException("Invalid")
Not Null Assertion Operator ( !! ) - If the value is null, the not null assertion (!!) operator changes it to a non-null type and throws an exception.
Anyone who wants a NullPointerException can ask for it explicitly with this operator.
For example,
// KOTLIN
fun main(args: Array) {
var sample : String? = null
str!!.length
}
The above code snippet gives the following output:-
Exception in thread "main" kotlin.KotlinNullPointerException