October 2018
Intermediate to advanced
370 pages
9h 15m
English
The not null assertion operator, also known as the sure operator, is used when it is sure that the provided variable always contains a value and is not null. Let's take a look at an example of how this works. Create a nullable string variable and assign a null value to it. Now try get the string's length with the null assertion operator:
fun main(args: Array<String>) { var sureNotNull : String? = null var length = sureNotNull!!.length // application will be crashed println("value of length is " + length)}
Of course, the application will crash. In this case, the programmer takes responsibility for variable nullability. Let's elaborate on this further with the another example.
The string class provides the lastOrNull function. ...
Read now
Unlock full access