0
0
Kotlinprogramming~10 mins

Nullable types with ? suffix in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Nullable types with ? suffix
Declare variable with ?
Variable can hold value or null
Check if variable is null
Handle null
Continue
This flow shows how a variable declared with ? can hold a value or null, and how the program checks and handles null before using the variable.
Execution Sample
Kotlin
var name: String? = "Alice"
println(name?.length)
name = null
println(name?.length)
This code declares a nullable String, prints its length safely, then sets it to null and prints length again safely.
Execution Table
StepVariable 'name'OperationResultOutput
1undefinedDeclare nullable String 'name' and assign "Alice"name = "Alice"
2"Alice"Print length safely using name?.lengthlength = 55
3"Alice"Assign null to 'name'name = null
4nullPrint length safely using name?.lengthlength = null (safe call)null
5nullEnd of program-
💡 Program ends after safely printing length of nullable variable twice.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
nameundefined"Alice""Alice"nullnullnull
Key Moments - 2 Insights
Why do we use the ?. operator when accessing length?
Because 'name' can be null, using ?. safely returns null instead of causing an error. See execution_table step 4 where name is null and length is safely null.
What happens if we try to access length without ? when name is null?
It causes a runtime error (NullPointerException). The ?. operator prevents this by checking for null first, as shown in steps 2 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the output when name is "Alice"?
AError
Bnull
C5
D0
💡 Hint
Check the Output column at step 2 in execution_table.
At which step does the variable 'name' become null?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the Variable 'name' column in execution_table to see when it changes to null.
If we remove the ? from name?.length in step 4, what would happen?
AOutput would be 0
BProgram would crash with error
COutput would be null
DOutput would be "null" string
💡 Hint
Recall key_moments about safe calls preventing errors when variable is null.
Concept Snapshot
Nullable types use ? after type name, e.g., String?.
Variables can hold a value or null.
Use ?. to safely access properties or methods.
Without ?, accessing null causes error.
Always check or use safe calls with nullable types.
Full Transcript
In Kotlin, adding a ? after a type means the variable can hold a value or null. For example, var name: String? means name can be a String or null. When using such variables, you must be careful to avoid errors. The ?. operator lets you safely access properties or methods; if the variable is null, it returns null instead of crashing. In the example, name starts as "Alice" and printing name?.length gives 5. After setting name to null, printing name?.length safely returns null without error. This helps prevent crashes from null values.