When you want to state that a reference type doesn’t point to a memory address, you can assign it a null value. If you have ever used Java, you probably experienced the NullPointerException
fail, as it is one of the most common errors. This error is raised by the runtime when you dereference a null variable.
This decision, to have nulls in Java, is criticized a lot and is also sometimes called a Billion Dollar mistake. The introduction of the optional type in Java 8, tries to address this problem.
Kotlin tries to prevent these mistakes with supporting nullability in its type system. With nullable types, Kotlin tries to prevent null pointer errors during compilation and reduce the possibility of null pointer errors during runtime.
All types in Kotlin can be nullable, and only then can they have a null value assigned. Nullable types are declared with a question mark at the end. First, take a look at this example where the compiler doesn’t allow a non-nullable type to be assigned...