August 2017
Intermediate to advanced
440 pages
10h
English
Another tool to deal with nullity is the not-null assertion operator. It is represented by a double exclamation mark (!!). This operator explicitly casts nullable variables to non-nullable variables. Here is a usage example:
var y: String? = "foo"
var size: Int = y!!.length
Normally, we would not be able to assign a value from a nullable property length to a non-nullable variable size. However, as developers, we can assure the compiler that this nullable variable will have a value here. If we are right, our application will work correctly, but if we are wrong, and the variable has a null value, the application will throw NullPointerException. Let's examine our activity method onCreate():
override fun onCreate(savedInstanceState: ...
Read now
Unlock full access