0
0
Kotlinprogramming~10 mins

Type checking with is operator in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Type checking with is operator
Start with variable
Check: variable is Type?
NoElse branch or skip
Yes
Execute code for that type
Continue or end
The program checks if a variable is a certain type using 'is'. If yes, it runs code for that type; if no, it skips or runs else.
Execution Sample
Kotlin
fun checkType(x: Any?) {
    if (x is String) {
        println("String of length ${x.length}")
    } else {
        println("Not a String")
    }
}
This function checks if x is a String and prints its length; otherwise, it prints 'Not a String'.
Execution Table
StepVariable xCondition (x is String)Branch TakenOutput
1Hellotrueif branchString of length 5
2123falseelse branchNot a String
3truefalseelse branchNot a String
4Kotlintrueif branchString of length 6
5-falseelse branchNot a String
💡 All inputs checked; function ends after printing output.
Variable Tracker
Variable xStartAfter 1After 2After 3After 4After 5
xHelloHello123trueKotlin-
Key Moments - 2 Insights
Why does the code inside 'if (x is String)' run only for some inputs?
Because the 'is' operator checks the actual type of x at runtime. Only when x is a String (rows 1 and 4) does the if branch run, as shown in the execution_table.
What happens if x is null? Does 'x is String' return true?
No, 'x is String' returns false if x is null, so the else branch runs (row 5). This is because null is not considered a String.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output when x = 123?
ANot a String
BString of length 3
CError
DNo output
💡 Hint
Check row 2 in execution_table where x is 123 and condition is false.
At which step does the condition 'x is String' become true?
AStep 2
BStep 1
CStep 3
DStep 5
💡 Hint
Look at the Condition column in execution_table for true values.
If x was changed to an Int value 10, what would be the output?
AString of length 2
BError
CNot a String
DNo output
💡 Hint
Refer to how non-String types like true or 123 produce 'Not a String' in execution_table.
Concept Snapshot
Type checking with 'is' operator in Kotlin:
- Syntax: if (variable is Type) { ... }
- Checks if variable is of given Type at runtime
- Runs code inside if only if true
- Else branch runs if false
- Null is not considered any type
Full Transcript
This visual trace shows how Kotlin's 'is' operator checks a variable's type at runtime. The function checkType takes any value x. It tests if x is a String. If yes, it prints the string length. If no, it prints 'Not a String'. The execution table shows different inputs: 'Hello' and 'Kotlin' are Strings, so the if branch runs and prints length. Inputs like 123, true, and null are not Strings, so else branch runs. The variable tracker shows how x changes each step. Key moments clarify why only Strings pass the check and that null is not a String. The quiz tests understanding of outputs and condition results. This helps beginners see how type checking controls program flow in Kotlin.