Start learning free

LESSON 20 OF 348 · CALCULATIONS WITH NUMBERS

Operator Precedence in Kotlin

Lesson Read the full explanation

TL ; DR:

  • When we string operations together, Kotlin must know which one to do first. This is called operator precedence.


  • * and / are performed before + and -:

  • println(1 + 2 * 3) // Prints 7
    println(6 - 4 / 2) // Prints 4
    println(5 * 4 - 2 / 2) // Prints 19
    

  • We can add () to force an operation to be performed first:

    println((1 + 2) * 3) // Prints 9
    println((6 - 4) / 2) // Prints 1
    println(5 * (4 - 2) / 2) // Prints 5
    


Full lesson:

When we string operations together, Kotlin must know which one to do first. This is called operator precedence.

This is the hierarchy from highest precedence to lowest precedence:

  • Parentheses are always respected
  • Exponentiation (raise to a power)
  • Multiplication, Division and Remainder
  • Addition and Subtraction
  • Left to right


First example:

println(1 + 2 * 3) // Output: 7

Multiplication is executed before addition, so:

1 + 2 * 3 = 1 + 6 = 7

Second example:

println((1 + 2) * 3) // Output: 9

What's inside parentheses is executed before multiplication, so:

(1 + 2) * 3 = 3 * 3 = 9

Third example:

println(6 / 4 * 3 + 5) // Output: 9.5

Multiplication and division come before addition. Also, multiplication and division have the same priorty so they are executed from left to right:

6 / 4 * 3 + 5 = 1.5 * 3 + 5 = 4.5 + 5 = 9.5

Forth example:

println(6 / ((4 + 2) / 3)) // Output: 3

Computer sees parentheses so it looks at it like this:

6 / ((4 + 2) / 3) = 6 / x, where x = (4 + 2) / 3

I have to compute x first. Parantheses are executed before division:
(4 + 2) / 3 = 6 / 3 = 2

I can replace x with 2:
6 / ((4 + 2) / 3) = 6 / 2 = 3


Assignment
Follow the Coding Tutorial and let's practice with operator precedence!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check

    KOTLIN · CALCULATIONS WITH NUMBERS

    Operator Precedence in Kotlin, explained

    What precedence means

    When an expression has several operators, Kotlin needs a rule for which one to run first. That rule is operator precedence. It works like the order of operations in mathematics: some operators bind more tightly than others, and parentheses override the default.

    From highest to lowest, the order that matters here is:

    1. Parentheses ( ).
    2. Multiplication, division and remainder: *, /, %.
    3. Addition and subtraction: +, -.

    When two operators share a level, Kotlin runs them from left to right. Kotlin has no operator for exponents, so there is no power level in this list.

    Examples

    println(1 + 2 * 3)        // 7
    println(6 - 4 / 2)        // 4
    println(5 * 4 - 2 / 2)    // 19
    

    In 1 + 2 * 3, the multiplication goes first: 1 + 6 is 7. In 5 * 4 - 2 / 2, both the multiplication and the division run before the subtraction, giving 20 - 1, which is 19.

    Parentheses change the order:

    println((1 + 2) * 3)         // 9
    println((6 - 4) / 2)         // 1
    println(5 * (4 - 2) / 2)     // 5
    println(6 / ((4 + 2) / 3))   // 3
    

    With nested parentheses, the innermost pair runs first. In the last line, (4 + 2) is 6, then 6 / 3 is 2, and finally 6 / 2 is 3.

    Same level, left to right

    Consider 6 / 4 * 3 + 5. Multiplication and division have equal precedence, so they run left to right: 6 / 4 first. Both numbers are integers, so the result is 1, and the fraction is dropped. Then 1 * 3 is 3, and 3 + 5 is 8. The program prints 8, not 9.5. To get 9.5, make one operand a Double:

    println(6 / 4 * 3 + 5)      // 8
    println(6.0 / 4 * 3 + 5)    // 9.5
    

    Subtraction behaves the same way: 10 - 4 - 3 is (10 - 4) - 3, which is 3. Reading it as 10 - (4 - 3) would give 9.

    The exercise

    The starting code is println(2 + 3 * 3), which prints 11, because the multiplication runs first: 2 + 9. The goal is to change the output to 15, so the addition has to happen before the multiplication.

    1. Find the sum that must go first. 15 is 5 times 3, and 5 is 2 + 3.
    2. Wrap that sum in parentheses. Parentheses beat multiplication, so (2 + 3) is computed before anything else.
    3. Keep the rest. The sum is then multiplied by the remaining 3.
    println((2 + 3) * 3)
    

    It prints 15. Only the parentheses were added; the numbers and operators are unchanged.

    More practice

    Work these out by hand first, then compare with the output:

    println(1 + 2 * 3 - 4 / 2)    // 5
    println(2 + 10 % 4)           // 4
    println((2 + 10) % 4)         // 0
    println(8 / 2 * (2 + 2))      // 16
    println(8 / (2 * (2 + 2)))    // 1
    

    The first line is 1 + 6 - 2. In the second, % runs first: 10 % 4 is 2, so the result is 4. In the third, the parentheses make it 12 % 4, which is 0. The fourth is 4 * 4, because after the parentheses the division and multiplication go left to right. The fifth groups the whole denominator, 2 * 4, so the result is 8 / 8.

    Common mistakes

    • Parenthesising the wrong part. 2 + (3 * 3) is the same as the original and still prints 11.
    • Reading strictly from left to right. Someone who computes 2 + 3 and then * 3 without parentheses gets 15 in their head, but Kotlin does not.
    • Unbalanced parentheses. Every ( needs a matching ), otherwise the code does not compile.
    • Trusting integer division in a chain. The fraction is dropped at the moment each division runs.

    Key takeaways

    • Parentheses first, then *, /, %, then +, -.
    • Operators at the same level run from left to right.
    • When unsure, add parentheses: they cost nothing and make the intent clear.

    Why the rules exist

    These rules match ordinary school mathematics, so a formula you write on paper usually translates directly into code. The one difference to remember is integer division: on paper, 6 / 4 is 1.5, but between two Int values Kotlin gives 1. Precedence decides which operation runs first, and the operand types decide what each operation returns. Keep both in mind whenever a result looks wrong.

    The remainder operator % shares a level with * and /, which is why 10 % 4 * 2 is 2 * 2, giving 4.

    Keep going with KotlinAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free