kotlin null safety code example
Example 1: !! in kotlin
val l = b!!.length
Example 2: elvis operator kotlin example
val l = b?.length ?: -1
Example 3: kotlin Null Safety
var neverNull: String = "This can't be null"
neverNull = null
var nullable: String? = "You can keep a null here"
nullable = null
var inferredNonNull = "The compiler assumes non-null"
inferredNonNull = null
fun strLength(notNull: String): Int {
return notNull.length
}
strLength(neverNull)
strLength(nullable)
Example 4: null check in kotlin
if(a != null) {
}