Overview - Nullable types with ? suffix
What is it?
Nullable types in Kotlin are types that can hold either a value or null. They are marked by adding a question mark (?) after the type name, for example, String?. This tells the program that the variable can safely hold a null value without causing errors. Nullable types help prevent common mistakes where null values cause crashes.
Why it matters
Without nullable types, programs often crash when they try to use a value that is actually null. This is called a null pointer exception and is a common source of bugs. Nullable types force programmers to think about and handle the possibility of null values, making programs safer and more reliable. This reduces crashes and unexpected behavior in apps.
Where it fits
Before learning nullable types, you should understand basic Kotlin types and variables. After mastering nullable types, you can learn about safe calls, the Elvis operator, and null checks to handle null values effectively. Nullable types are a foundation for writing robust Kotlin code.