The ?.  Safe call operator  

An alternative way to verify the nullable variable is by using the safe call operator, which is a question mark followed by a full stop, ?.

var length = mayBeNull?.length

In this case, if the string variable is not null, the safe call operator will return the length of the variable. Otherwise, null will be assigned to the length variable. This means that when the if variable is null, everything after the ?. operator will be ignored:

fun main(args: Array<String>) {
var mayBeNull : String?
mayBeNull = null // allowed
var length = mayBeNull?.length // Safe Call
println("value of length is " + length)
}

The output of this example is "value of length is null" because the safe call operator verified the mayBeNull variable and returned a null value. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.99.71