Introduction to Nullability
Explore Kotlin's solutions to nullability issues, including explicit rules and versatile tools for efficient null management.
We'll cover the following
Understanding the need for Kotlin
Kotlin started as a remedy to Java problems, and one of the biggest problems in Java is nullability. In Java and many other languages, every variable is nullable, so it might have the null
value. Every call on a null
value leads to the famous NullPointerException
(
I call it my billion-dollar mistake. It was the invention of the null reference in 1965… This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years.
Kotlin’s solution
One of Kotlin’s priorities was to finally solve this problem, and it achieved this perfectly. The mechanisms introduced in Kotlin are so effective that seeing NullPointerException
thrown from Kotlin code is extremely rare. The null
value stopped being a problem, and Kotlin developers are no longer scared of it. It has become our friend.
Nullability rules
So, how does nullability work in Kotlin? Everything is based on a few rules:
Every property needs to have an explicit value. There is no such thing as an implicit
null
value.
Get hands-on with 1200+ tech skills courses.