Overview - Non-nullable types by default
What is it?
Non-nullable types by default means that in Kotlin, variables cannot hold a null value unless explicitly declared. This helps prevent errors caused by null values, which are common in many programming languages. By default, every variable must have a valid value, making the code safer and more predictable. To allow nulls, Kotlin requires a special syntax that clearly marks variables as nullable.
Why it matters
Null values often cause programs to crash unexpectedly, leading to frustrating bugs called null pointer exceptions. By making types non-nullable by default, Kotlin forces developers to think about nulls explicitly, reducing these bugs. Without this concept, programs would be less reliable and harder to maintain, causing wasted time and unhappy users.
Where it fits
Before learning this, you should understand basic Kotlin types and variables. After mastering non-nullable types, you can learn about nullable types, safe calls, and Kotlin's null safety operators. This concept is foundational for writing robust Kotlin code and understanding how Kotlin improves safety compared to other languages.