Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Null Safety

Nulls are a feature of many programming languages. They can be useful, as a way of indicating the absence of a meaningful value, but they can also cause problems. A common run-time error in Java applications, for example, is the null pointer exception (NPE).

One of Kotlin’s most compelling features is that it can handle nulls safely and elegantly. As a result, NPEs rarely occur in Kotlin applications.

After completing this section, you will understand why it is important for Kotlin to be strict about how null values are handled. You will also be able to work with nullable types in your code and perform the required null checks efficiently.