February 2018
Intermediate to advanced
350 pages
7h 35m
English
One of the main features of Kotlin is nullable types. Nullable types allow us to define if a value can contain or being null explicitly:
fun main(args: Array<String>) { val myBlueberryCupcake: Cupcake = null //Compilation error: Null can not be a value of a non-null type Cupcake}
This isn't valid in Kotlin; the Cupcake type doesn't allow null values. To allow null values, myBlueberryCupcake must have a different type:
fun main(args: Array<String>) { val myBlueberryCupcake: Cupcake? = null}
In essence, Cupcake is a non-null type and Cupcake? is a nullable type.
In the hierarchical structure, Cupcake is a subtype of Cupcake?. So, in any situation where Cupcake? is defined, Cupcake can be used, but not the other way around:
Read now
Unlock full access