not null assertion (!!) operator


The not null assertion (!!) operator converts any value to a non-null type and throws an exception if the value is null.

If anyone want NullPointerException then he can ask explicitly using this operator.

fun main(args: Array<String>) {
   var str : String? = "GeeksforGeeks"
   println(str!!.length)
   str = null
   str!!.length
}

Output

Exception in thread "main" kotlin.KotlinNullPointerException at FirstappKt.main(firstapp.kt:8)




Comments

Popular Posts