October 2018
Intermediate to advanced
370 pages
9h 15m
English
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 ...
Read now
Unlock full access