Null safety helps prevent errors when a program tries to use something that is missing or empty. Kotlin makes this easy and safe by design.
Why null safety is Kotlin's defining feature
var name: String = "John" // cannot be null var nickname: String? = null // can be null
Use ? after the type to allow a variable to hold null.
Without ?, Kotlin will not allow null values, preventing errors.
var message: String = "Hello" message = null // Error: Null can not be a value of a non-null type String
? allows the variable to hold null safely.var message: String? = "Hello" message = null // This is allowed
?. to safely access properties when the variable might be null.val length = message?.length // Safe call operator returns length or null
!! to assert a variable is not null, but it can crash if wrong.val length = message!!.length // Throws error if message is null
This program shows how Kotlin handles null safety. It prints the length of a non-null name, then safely tries to print the length of a nullable nickname which starts as null, then changes to a value.
fun main() { var name: String = "Alice" println("Name length: ${name.length}") var nickname: String? = null println("Nickname length: ${nickname?.length}") nickname = "Ally" println("Nickname length: ${nickname?.length}") }
Null safety helps avoid the common 'null pointer exception' errors found in many languages.
Using ? and safe calls ?. makes your code more reliable and easier to maintain.
Be careful with !! because it can cause crashes if the value is actually null.
Kotlin's null safety prevents many common bugs by making null explicit.
Use ? to allow nulls and safe calls ?. to handle them safely.
This feature makes Kotlin code safer and easier to understand.
Practice
Solution
Step 1: Understand null safety concept
Kotlin's null safety is designed to avoid null pointer exceptions by making null handling explicit.Step 2: Identify the main purpose
The main goal is to prevent errors caused by accessing null values, which are common bugs in many languages.Final Answer:
To prevent errors caused by accessing null values -> Option BQuick Check:
Null safety = prevent null errors [OK]
- Thinking null safety allows all variables to be null
- Believing it improves performance directly
- Confusing null safety with removing declarations
Solution
Step 1: Recall Kotlin nullable syntax
In Kotlin, adding a question mark after the type makes it nullable, allowing it to hold null.Step 2: Check each option
val name: String? = null usesString?which is correct. Others are invalid syntax or do not allow null.Final Answer:
val name: String? = null -> Option AQuick Check:
Nullable type uses ? after type [OK]
- Omitting the ? for nullable types
- Using invalid symbols like ! or ?null
- Assigning null to non-nullable types
val name: String? = null println(name?.length ?: "No name")
Solution
Step 1: Understand safe call and Elvis operator
The safe call?.returns null ifnameis null. The Elvis operator?:provides a default value if the left side is null.Step 2: Evaluate the expression
Sincenameis null,name?.lengthis null, so the expression returns "No name".Final Answer:
No name -> Option DQuick Check:
Safe call + Elvis = default value [OK]
- Expecting NullPointerException
- Thinking length of null is 0
- Confusing safe call with normal call
val length = name.length val name: String? = null
Solution
Step 1: Analyze variable usage order
The variablenameis used before it is declared.Step 2: Identify the compile error
Kotlin requires variables to be declared before use, causing an unresolved reference error.Final Answer:
Variable 'name' is used before declaration -> Option AQuick Check:
Declare before use [OK]
- Thinking it's only a null safety issue
- Believing the code runs fine
- Assuming Kotlin requires semicolons
printLength that prints the length of a nullable String text without causing a NullPointerException?Solution
Step 1: Understand function parameter nullability
To accept null, parameter must beString?. Options A and D use non-nullableString, so passing null causes error.Step 2: Use safe call and default value
fun printLength(text: String?) { println(text?.length ?: 0) } printLength(null) uses safe call?.and Elvis operator?:to print 0 iftextis null, avoiding exceptions.Final Answer:
fun printLength(text: String?) { println(text?.length ?: 0) } printLength(null) -> Option CQuick Check:
Safe call + Elvis handles null safely [OK]
- Passing null to non-nullable parameter
- Accessing length without safe call
- Ignoring default value for null case
Ready to prove it?
Pass the test and this pattern moves unknown
