What if your code's math is secretly wrong because you ignored operator precedence?
Why Operator precedence in Kotlin? - Purpose & Use Cases
Imagine you are solving a math problem by hand, mixing addition, multiplication, and subtraction without clear rules. You write down the operations in the order they appear, hoping the answer is right.
This manual way is slow and confusing. You might add before multiplying or subtract before adding, leading to wrong answers. It's easy to make mistakes and hard to check your work.
Operator precedence gives clear rules about which operations to do first. It's like having a priority list so your calculations always follow the same order, making your code correct and easier to understand.
val result = 2 + 3 * 4 - 5 // What is the result?
val result = 2 + (3 * 4) - 5 // Multiplication happens before addition and subtraction
With operator precedence, you can write complex expressions confidently, knowing they will be evaluated correctly every time.
When calculating a shopping bill with discounts and taxes, operator precedence ensures you apply multiplication for tax before adding the discount, so the final price is accurate.
Manual calculation order can cause errors.
Operator precedence sets clear rules for operation order.
This makes code reliable and easier to read.
Practice
3 + 4 * 2?Solution
Step 1: Identify operators in the expression
The expression has addition (+) and multiplication (*).Step 2: Recall operator precedence rules
Multiplication (*) has higher precedence than addition (+), so it runs first.Final Answer:
Multiplication (*) -> Option AQuick Check:
Multiplication before addition = Multiplication (*) [OK]
- Thinking addition runs before multiplication
- Ignoring operator precedence
- Assuming left to right always applies
3 + 4 * 2 so that addition happens first?Solution
Step 1: Understand default precedence
Without parentheses, multiplication runs before addition.Step 2: Check which option forces addition first
(3 + 4) * 2 uses parentheses around (3 + 4), so addition happens before multiplication.Final Answer:
(3 + 4) * 2 -> Option BQuick Check:
Parentheses change order = (3 + 4) * 2 [OK]
- Ignoring parentheses effect
- Choosing expression without parentheses
- Assuming left to right overrides precedence
val result = 10 - 4 + 2 * 3 println(result)
Solution
Step 1: Apply operator precedence
Multiplication (*) runs first: 2 * 3 = 6.Step 2: Evaluate remaining expression left to right
10 - 4 + 6 = 6 + 6 = 12.Final Answer:
12 -> Option AQuick Check:
Multiplication before addition/subtraction = 12 [OK]
- Adding before multiplying
- Calculating left to right ignoring precedence
- Miscomputing subtraction and addition order
val x = 5 + 3 * 2 && true
What is the problem?
Solution
Step 1: Identify mixed types in expression
The expression mixes arithmetic (5 + 3 * 2) and boolean (&& true) without separation.Step 2: Understand Kotlin type rules
You cannot combine Int and Boolean directly without explicit logic or parentheses.Final Answer:
Cannot mix arithmetic and boolean operators without parentheses -> Option DQuick Check:
Mixing Int and Boolean needs care [OK]
- Thinking semicolon is missing
- Assuming && works with numbers
- Ignoring type mismatch errors
val result = true || false && false || true && false
What is the value of
result considering operator precedence?Solution
Step 1: Recall logical operator precedence
Logical AND (&&) has higher precedence than OR (||), so evaluate && first.Step 2: Evaluate all AND operations
false && false = false, true && false = false.Step 3: Replace and evaluate OR operations left to right
true || false || false = true || false = true.Final Answer:
true -> Option CQuick Check:
AND before OR, final result true [OK]
- Evaluating OR before AND
- Confusing operator precedence
- Assuming expression returns null or error
Ready to prove it?
Pass the test and this pattern moves unknown
