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:
- Parentheses
( ). - Multiplication, division and remainder:
*,/,%. - 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.
- Find the sum that must go first. 15 is 5 times 3, and 5 is
2 + 3. - Wrap that sum in parentheses. Parentheses beat multiplication, so
(2 + 3)is computed before anything else. - 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 + 3and then* 3without 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.