📖 Scenario: Imagine you are creating a simple contact list app. Some contacts have phone numbers, but some do not. You want to store phone numbers in a way that allows missing numbers without causing errors.
🎯 Goal: Learn how to use nullable types with the ? suffix in Kotlin to safely handle values that might be missing.
📋 What You'll Learn
Create a variable with a nullable type using the ? suffix
Assign a value and null to the nullable variable
Use safe call operator ?. to access the nullable variable
Print the results to see how Kotlin handles nullable types
💡 Why This Matters
🌍 Real World
Nullable types help apps handle missing or optional data safely, like missing phone numbers in contacts.
💼 Career
Understanding nullable types is essential for Kotlin developers to avoid crashes and write safe, reliable code.
Progress0 / 4 steps
1
Create a nullable variable
Create a variable called phoneNumber of type String? and assign it the value "123-456-7890".
Kotlin
Hint
Use String? to make the variable nullable.
2
Assign null to the nullable variable
Change the value of phoneNumber to null.
Kotlin
Hint
Just assign null to the variable.
3
Use safe call operator to access nullable variable
Create a variable called length and assign it the length of phoneNumber using the safe call operator ?..
Kotlin
Hint
Use ?. to safely get length without error if phoneNumber is null.
4
Print the length variable
Print the value of length using println.
Kotlin
Hint
Just print length to see the result.
Practice
(1/5)
1. What does adding a ? after a type in Kotlin mean?
easy
A. The variable is immutable.
B. The variable can hold a null value.
C. The variable is a constant.
D. The variable is a function.
Solution
Step 1: Understand Kotlin type suffix
In Kotlin, adding ? after a type means the variable can hold null.
Step 2: Compare with non-nullable types
Without ?, variables cannot be null and will cause errors if assigned null.
Final Answer:
The variable can hold a null value. -> Option B
Quick Check:
Nullable type = variable can be null [OK]
Hint: Remember: ? means nullable type in Kotlin [OK]
Common Mistakes:
Thinking ? makes variable immutable
Confusing ? with constant declaration
Assuming ? means function type
2. Which of the following is the correct syntax to declare a nullable String variable in Kotlin?
easy
A. var name: String?
B. var name: ?String
C. var name: String!
D. var name: nullable String
Solution
Step 1: Recall Kotlin nullable syntax
Kotlin uses ? after the type name to mark it nullable, e.g., String?.
Step 2: Check each option
var name: String? uses String? correctly. Options A, B, and D use invalid syntax.
Final Answer:
var name: String? -> Option A
Quick Check:
Nullable syntax = type followed by ? [OK]
Hint: Put ? right after the type name for nullable [OK]
Common Mistakes:
Placing ? before the type
Using ! instead of ?
Writing 'nullable' keyword explicitly
3. What will be the output of this Kotlin code?
var text: String? = null
println(text?.length ?: "No length")
medium
A. 0
B. null
C. No length
D. Throws NullPointerException
Solution
Step 1: Understand safe call and Elvis operator
text?.length safely accesses length if text is not null; else returns null. The Elvis operator ?: provides "No length" if left side is null.
Step 2: Evaluate given code
Since text is null, text?.length is null, so the expression prints "No length".
4. Identify the error in this Kotlin code snippet:
var number: Int? = 10
var result: Int = number + 5
medium
A. Variable 'result' should be nullable.
B. Variable 'number' cannot be nullable.
C. Missing semicolon after declaration.
D. Cannot add Int? and Int directly.
Solution
Step 1: Check type compatibility
number is nullable Int?, but result expects non-null Int. Adding nullable and non-null directly is not allowed.
Step 2: Understand Kotlin's null safety
You must safely unwrap number before adding, e.g., using number ?: 0 or safe call.
Final Answer:
Cannot add Int? and Int directly. -> Option D
Quick Check:
Nullable + non-null Int needs safe unwrap [OK]
Hint: Nullable Int can't be used directly in arithmetic [OK]
Common Mistakes:
Assuming nullable Int can be used like Int
Ignoring need for safe call or Elvis operator
Thinking semicolon is required in Kotlin
5. You have a nullable list of nullable Strings: val items: List?. How do you safely print the length of the first item or print 0 if the list or first item is null?
hard
A. println(items?.get(0)?.length ?: 0)
B. println(items[0].length)
C. println(items!!.get(0)!!.length)
D. println(items?.get(0).length ?: 0)
Solution
Step 1: Analyze nullable types
items can be null, and its elements can be null. So both list and first item need safe calls.
Step 2: Use safe calls and Elvis operator
items?.get(0)?.length ?: 0 safely accesses first item length or returns 0 if null.