0
0
Kotlinprogramming~10 mins

Extensions on built-in types in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Extensions on built-in types
Start
Define extension function
Call extension on built-in type
Execute extension code
Return result
End
We define a new function for a built-in type, then call it like a normal method, and it runs the added code.
Execution Sample
Kotlin
fun String.shout() = this.uppercase() + "!"

val word = "hello"
println(word.shout())
This code adds a shout() function to String that makes it uppercase and adds an exclamation.
Execution Table
StepActionExpressionResultOutput
1Define extension function shout on Stringfun String.shout() = this.uppercase() + "!"Function ready
2Assign wordval word = "hello"word = "hello"
3Call extension shout on wordword.shout()word.uppercase() + "!"
4Evaluate uppercase"hello".uppercase()"HELLO"
5Concatenate !"HELLO" + "!""HELLO!"
6Print resultprintln("HELLO!")HELLO!
💡 Program ends after printing the shouted word.
Variable Tracker
VariableStartAfter 1After 2Final
wordundefinedundefined"hello""hello"
result of word.shout()undefinedundefinedundefined"HELLO!"
Key Moments - 2 Insights
Why can we call shout() on a String even though String has no shout() method?
Because shout() is an extension function defined for String (see execution_table step 1). Kotlin lets us add functions to existing types without changing their code.
Does the original String change when we call shout()?
No, the original String 'word' stays "hello" (see variable_tracker). The extension returns a new String with uppercase and exclamation.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4. What is the result of "hello".uppercase()?
A"HELLO"
B"hello"
C"Hello"
D"HELLO!"
💡 Hint
Check the 'Result' column in execution_table row 4.
At which step is the extension function shout() defined?
AStep 2
BStep 3
CStep 1
DStep 6
💡 Hint
Look at the 'Action' column in execution_table for the function definition.
If we changed shout() to add "!!!" instead of "!", what would be the output at step 6?
A"HELLO!"
B"HELLO!!!"
C"hello!!!"
D"hello!"
💡 Hint
Refer to execution_table steps 5 and 6 where concatenation and print happen.
Concept Snapshot
Extensions on built-in types let you add new functions to existing types.
Syntax: fun TypeName.newFunction() { ... }
Call like normal: value.newFunction()
Does not modify original type, just adds callable functions.
Useful to add behavior without inheritance or modifying source.
Full Transcript
This example shows how Kotlin lets you add new functions to built-in types like String using extensions. We define shout() to uppercase the string and add an exclamation mark. Then we call shout() on a String variable 'word'. The program runs step-by-step: defining the function, assigning the variable, calling the extension, computing uppercase, concatenating, and printing the result. The original string stays unchanged. This technique helps add new behavior to existing types easily.