Operator precedence and parentheses
20 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)Operator precedence is the order in which Java runs the operators in one line. * runs before +, so 2 + 3 * 4 is 14, not 20.
Which operator runs first?
Two friends read 2 + 3 * 4. One says 20, the other says 14.
Operator precedence is Java's fixed order, like BODMAS in school maths. The list shows it for this module's operators, first to last.
- Parentheses
( ) ++,--,!*,/,%+,-<,>,<=,>=, then==,!=&&, then||, then?:, and last=,+=,-=
Example 1 · Multiply before add, left to right
public class Order { public static void main(String[] args) { System.out.println(2 + 3 * 4); System.out.println(10 + 8 / 2); System.out.println(20 - 5 - 3); System.out.println(17 % 5 * 2); }}14 14 12 4
What this code does
*and/run before+, so2 + 3 * 4is 14 and10 + 8 / 2is 14.- Same level runs left to right:
20 - 5first, and17 % 5(which is 2) before* 2.
Only = and ?: group right to left, so a = b = 5; stores 5 in b first, then in a. Java has a few more groups that this course does not use, so this is a simplified table.
Parentheses make it clear
You want the average of three marks and write a + b + c / 3, but the answer comes out far too big.
Parentheses ( ), also called round brackets, always run first: (a + b + c) / 3 adds, then divides.
Example 2 · Average marks needs parentheses
public class Average { public static void main(String[] args) { int a = 80; int b = 70; int c = 90; System.out.println(a + b + c / 3); System.out.println((a + b + c) / 3); }}180 80
What this code does
- Without brackets, only
c / 3(30) is divided: 80 + 70 + 30 = 180. - With parentheses, the sum 240 is divided by 3, which gives the real average, 80.
When you are not sure about the order, add brackets; they also make the code easier to read. Try the lab: the same two numbers give different answers with and without brackets.
Experiment
Brackets lab
Change a and b. Compare each line with its bracketed twin.
- a + b * 2
- 16
- (a + b) * 2
- 20
- a + b / 2
- 7
- (a + b) / 2
- 5
Trace one expression step by step
A long line like 5 + 3 * 4 - 6 / 2 is hard to work out in one go. Java does it one operator at a time, and you can too.
public class TraceTotal { public static void main(String[] args) { int total = 5 + 3 * 4 - 6 / 2; System.out.println(total); }}14
First * and /, from left to right; then + and -, from left to right; and = last. Step through the diagram: total gets 14.
5 + 3 * 4 - 6 / 2, one step at a time
Step 1 of 5
* and / run first, left to right. The first one is 3 * 4.+ with text and numbers
A scoreboard prints "Total: " + runs1 + runs2 and shows Total: 4530 in place of 75.
+ runs left to right, as in Text with String (opens in a new tab). Once the left side is text, every + after it joins text.
Example 3 · Text and numbers with +
public class Runs { public static void main(String[] args) { int runs1 = 45; int runs2 = 30; System.out.println("Total: " + runs1 + runs2); System.out.println("Total: " + (runs1 + runs2)); System.out.println(runs1 + runs2 + " runs"); System.out.println("Fifty: " + (runs1 >= 50)); }}Total: 4530 Total: 75 75 runs Fifty: false
What this code does
- Text comes first in the first line, so 45 and 30 are joined as text:
4530. - In
runs1 + runs2 + " runs", the numbers come first, so they are added before the text joins. - The comparison has its own brackets, so it runs before the text is joined.
"Passed: " + marks >= 35 does not compile: Java joins the text first, then tries to compare text with 35, and says bad operand types for binary operator '>='. Write "Passed: " + (marks >= 35).
Next step
Check your understanding
10 quick questions