0
0
Kotlinprogramming~10 mins

This vs it receiver difference in Kotlin - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - This vs it receiver difference
Start function with receiver
Use 'this' to refer to receiver object
Use 'it' in lambda for implicit parameter
Perform actions using 'this' or 'it'
Return or end function
Shows how 'this' refers to the receiver object in extension functions, while 'it' is the implicit name for lambda parameters.
Execution Sample
Kotlin
fun String.showThis() {
    println(this)       // 'this' is the String receiver
    listOf(1, 2).forEach {
        println(it)     // 'it' is each element in the list
    }
}
Prints the receiver string using 'this' and each list element using 'it' inside a lambda.
Execution Table
StepCode LineReceiver/Object'this' Value'it' ValueOutput
1fun String.showThis() {String receiverString receiverN/ANo output
2println(this)String receiverString receiverN/AHello
3listOf(1, 2).forEach {List<Int>String receiver1No output
4println(it)List<Int>String receiver11
5forEach next iterationList<Int>String receiver2No output
6println(it)List<Int>String receiver22
7End of functionString receiverString receiverN/ANo output
💡 Function ends after printing receiver string and all list elements.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6Final
this (receiver)N/A"Hello""Hello""Hello""Hello"
it (lambda param)N/AN/A12N/A
Key Moments - 2 Insights
Why does 'this' refer to the string but 'it' refers to list elements?
'this' refers to the receiver object of the extension function (the string), while 'it' is the implicit name for each element inside the lambda passed to forEach (the list elements). See execution_table rows 2 and 4.
Can 'this' be used inside the lambda instead of 'it'?
No, inside the lambda 'this' still refers to the outer receiver (the string), not the lambda parameter. 'it' is used for the lambda's current element. See execution_table rows 4 and 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'this' at step 4?
A2
B1
C"Hello"
DN/A
💡 Hint
Check the 'this' Value column at step 4 in execution_table.
At which step does 'it' first get the value 2?
AStep 6
BStep 3
CStep 5
DStep 4
💡 Hint
Look at the 'it' Value column in execution_table rows 4 and 6.
If we replaced 'println(it)' with 'println(this)' inside the lambda, what would print?
AEach list element (1, then 2)
BThe receiver string each time
CNothing
DCompilation error
💡 Hint
Refer to key_moments about 'this' inside lambda and execution_table rows 4 and 6.
Concept Snapshot
In Kotlin extension functions, 'this' refers to the receiver object.
Inside lambdas, 'it' is the implicit name for the single parameter.
'this' inside a lambda still points to the outer receiver.
Use 'it' to access lambda elements.
This distinction helps avoid confusion when nesting lambdas.
Full Transcript
This visual shows how 'this' and 'it' work differently in Kotlin. 'this' refers to the receiver object of an extension function, like a string. When you use a lambda, like in forEach, 'it' is the implicit name for each element in the list. The execution table traces each step: first printing the receiver string with 'this', then printing each list element with 'it'. The variable tracker shows 'this' stays the same, while 'it' changes per element. Key moments explain why 'this' and 'it' are different and why you can't use 'this' to mean the lambda parameter. The quiz tests understanding by asking about values of 'this' and 'it' at different steps and what happens if you swap them. Remember, 'this' is the receiver, 'it' is the lambda parameter.