0
0
Kotlinprogramming~10 mins

Why operators are functions in Kotlin - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why operators are functions in Kotlin
Start with expression
Operator seen: +
Translate + to function call
Call function plus() on object
Function executes and returns result
Use returned result in expression
Kotlin treats operators like + as calls to functions, so using an operator is just calling a function behind the scenes.
Execution Sample
Kotlin
val a = 5
val b = 3
val c = a + b // calls a.plus(b)
This code adds two numbers using +, which Kotlin turns into a function call plus() on a.
Execution Table
StepCode LineActionFunction CalledResult
1val a = 5Assign 5 to aNonea = 5
2val b = 3Assign 3 to bNoneb = 3
3val c = a + bDetect + operatorplusCall a.plus(b)
4val c = a + bplus function adds valuesplusReturns 8
5val c = a + bAssign result to cNonec = 8
💡 Expression evaluated, c holds the sum 8
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 5
aundefined555
bundefinedundefined33
cundefinedundefinedundefined8
Key Moments - 2 Insights
Why does Kotlin treat + like a function call?
Because operators are just special function calls like plus(), Kotlin can let you define how + works for your own types. See step 3 and 4 in execution_table.
Is + a special keyword or a function in Kotlin?
It looks like a symbol but Kotlin converts it to a function call internally, so + is actually calling plus() on the object. See execution_table step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what function is called when using the + operator?
Asum
Bplus
Cadd
DoperatorPlus
💡 Hint
Check the 'Function Called' column at step 3 and 4 in execution_table.
At which step is the result of a.plus(b) assigned to variable c?
AStep 3
BStep 4
CStep 5
DStep 2
💡 Hint
Look at the 'Action' column describing assignment to c in execution_table.
If we replaced + with a custom operator, what would Kotlin do?
ACall a different function matching that operator
BThrow a syntax error
CIgnore the operator and do nothing
DAutomatically convert it to plus()
💡 Hint
Kotlin maps operators to functions, so a new operator means calling a matching function (see concept_flow).
Concept Snapshot
In Kotlin, operators like + are actually function calls.
Using a + b calls a.plus(b) behind the scenes.
This lets you define how operators work for your own types.
Operators are just special syntax for functions.
This design makes Kotlin flexible and consistent.
Full Transcript
Kotlin treats operators such as + as function calls. When you write a + b, Kotlin translates it to a call to the function plus() on the object a with b as argument. This means operators are not special keywords but functions with special syntax. This allows Kotlin to let you define how operators behave for your own classes by defining functions like plus(). The execution steps show assigning values to variables a and b, then calling plus() when + is used, and finally assigning the result to c. This approach makes Kotlin flexible and consistent in handling operations.