Bird
Raised Fist0
Kotlinprogramming~10 mins

Why operators are functions in Kotlin - Visual Breakdown

Choose your learning style10 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.

Practice

(1/5)
1. Why does Kotlin treat operators like functions?
easy
A. To allow customizing operator behavior for different types
B. Because Kotlin does not support functions
C. To make the code run faster
D. Because operators are only used in math

Solution

  1. Step 1: Understand Kotlin's operator design

    Kotlin treats operators as functions so you can define how they behave for your own classes.
  2. Step 2: Recognize the benefit

    This makes code flexible and readable by allowing operator overloading.
  3. Final Answer:

    To allow customizing operator behavior for different types -> Option A
  4. Quick Check:

    Operators as functions = Custom behavior [OK]
Hint: Operators are functions to customize behavior easily [OK]
Common Mistakes:
  • Thinking operators are only for math
  • Believing Kotlin lacks functions
  • Assuming operators make code faster
2. Which of these is the correct way to declare an operator function for plus (+) in Kotlin?
easy
A. operator fun plus(other: MyClass): MyClass
B. fun plus(other: MyClass): MyClass
C. fun operator plus(other: MyClass): MyClass
D. operator plus fun(other: MyClass): MyClass

Solution

  1. Step 1: Recall Kotlin operator function syntax

    Operator functions must use the keyword operator before fun.
  2. Step 2: Check the options

    Only operator fun plus(other: MyClass): MyClass correctly places operator before fun and defines the function properly.
  3. Final Answer:

    operator fun plus(other: MyClass): MyClass -> Option A
  4. Quick Check:

    operator keyword before fun = correct syntax [OK]
Hint: Use 'operator fun' to declare operator functions [OK]
Common Mistakes:
  • Placing 'operator' after 'fun'
  • Omitting 'operator' keyword
  • Misordering keywords
3. What will be the output of this Kotlin code?
data class Point(val x: Int, val y: Int) {
    operator fun plus(other: Point) = Point(x + other.x, y + other.y)
}

fun main() {
    val p1 = Point(1, 2)
    val p2 = Point(3, 4)
    val p3 = p1 + p2
    println(p3)
}
medium
A. Point(4, 6)
B. Point(x=4, y=6)
C. Error: Operator '+' not defined
D. Point(x=1, y=2) + Point(x=3, y=4)

Solution

  1. Step 1: Understand operator plus function

    The plus operator adds x and y values of two Point objects and returns a new Point.
  2. Step 2: Calculate the result

    p1 + p2 = Point(1+3, 2+4) = Point(4, 6). Printing a data class shows property names.
  3. Final Answer:

    Point(x=4, y=6) -> Option B
  4. Quick Check:

    p1 + p2 = Point(x=4, y=6) [OK]
Hint: Data class print shows property names [OK]
Common Mistakes:
  • Expecting Point(4, 6) without property names
  • Thinking '+' is undefined
  • Printing the expression instead of result
4. Identify the error in this Kotlin code that tries to overload the minus (-) operator:
class Counter(var count: Int) {
    fun operator minus(other: Counter): Counter {
        return Counter(count - other.count)
    }
}
medium
A. Function must return Int, not Counter
B. Missing 'operator' keyword before 'fun'
C. Minus operator cannot be overloaded
D. Incorrect order: 'fun operator' should be 'operator fun'

Solution

  1. Step 1: Check operator function syntax

    The 'operator' keyword must come before 'fun' in Kotlin.
  2. Step 2: Identify the error in code

    The code uses 'fun operator' which is incorrect; it should be 'operator fun'.
  3. Final Answer:

    Incorrect order: 'fun operator' should be 'operator fun' -> Option D
  4. Quick Check:

    operator keyword before fun = correct syntax [OK]
Hint: Put 'operator' before 'fun' keyword [OK]
Common Mistakes:
  • Placing 'operator' after 'fun'
  • Thinking minus can't be overloaded
  • Wrong return type assumption
5. You want to create a Kotlin class Vector that supports both addition (+) and multiplication (*) operators. How should you design these operators as functions to keep your code clean and flexible?
hard
A. Create separate classes for addition and multiplication
B. Use global functions without 'operator' keyword
C. Define 'operator fun plus' and 'operator fun times' inside Vector class
D. Override '+' and '*' symbols directly without functions

Solution

  1. Step 1: Understand operator overloading in Kotlin

    Kotlin requires operator functions inside the class or as extensions with the 'operator' keyword.
  2. Step 2: Choose the clean design

    Defining 'operator fun plus' and 'operator fun times' inside Vector class keeps code organized and flexible.
  3. Final Answer:

    Define 'operator fun plus' and 'operator fun times' inside Vector class -> Option C
  4. Quick Check:

    Operator functions inside class = clean, flexible [OK]
Hint: Put operator functions inside class with 'operator' keyword [OK]
Common Mistakes:
  • Trying to override symbols directly
  • Omitting 'operator' keyword
  • Using global functions without operator keyword
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard