0
0
Kotlinprogramming~10 mins

Safe call operator (?.) in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Safe call operator (?.)
Start
Check if object is null
Yes|No
Return null
Continue with result
The safe call operator checks if an object is null before accessing its property or method, avoiding errors by returning null if the object is null.
Execution Sample
Kotlin
val name: String? = null
val length = name?.length
println(length)
This code safely accesses the length of a nullable string, printing null if the string is null.
Execution Table
StepExpressionObject Null?ActionResult
1val name: String? = nullN/AAssign null to namename = null
2name?.lengthYesReturn null instead of accessing lengthlength = null
3println(length)N/APrint length valueOutput: null
💡 Safe call returns null because name is null, avoiding error.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
nameundefinednullnullnull
lengthundefinedundefinednullnull
Key Moments - 2 Insights
Why does length become null instead of causing an error?
Because the safe call operator (?.) checks if name is null before accessing length. In execution_table step 2, since name is null, it returns null safely.
What happens if name is not null?
If name is not null, the safe call operator accesses the property normally, as shown in execution_table step 2's 'No' branch in concept_flow.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the value of length?
A0
Bnull
CThrows error
DUndefined
💡 Hint
Check the 'Result' column in step 2 of execution_table where length is assigned.
At which step does the safe call operator prevent an error?
AStep 2
BStep 3
CStep 1
DNo step
💡 Hint
Look at the 'Action' column in step 2 where it returns null instead of accessing length.
If name was "Hello", what would length be at step 2?
AThrows error
Bnull
C5
DUndefined
💡 Hint
Recall that "Hello" has 5 characters; safe call accesses length normally if not null.
Concept Snapshot
Safe call operator (?.) in Kotlin:
- Used to safely access properties or methods of nullable objects.
- Syntax: object?.property or object?.method()
- Returns null if object is null, avoiding NullPointerException.
- Useful for chaining calls on nullable types.
- Example: val length = name?.length
- If name is null, length is null; else length is name's length.
Full Transcript
The safe call operator (?.) in Kotlin helps avoid errors when working with nullable objects. It checks if the object is null before accessing its property or method. If the object is null, it returns null instead of causing an error. For example, if a nullable string 'name' is null, then 'name?.length' safely returns null. This prevents the program from crashing due to null pointer exceptions. The execution table shows step-by-step how the variable 'length' becomes null safely. This operator is very useful when you want to work with nullable data without writing extra null checks.