Skip to content

Java Operator Precedence

Updated 7 min read Aimerscrew

What does 2 + 3 * 4 give in Java: 20 or 14? The answer depends on Java operator precedence, the set of rules that decides which operator runs first. In this lesson, you'll learn those rules, see a full precedence table, watch an expression get evaluated step by step, and find out why "Total: " + 2 + 3 prints Total: 23.

🌍 Real-life example

A café receipt says: 3 muffins at $2 each, plus a $4 coffee. Everyone knows the total is 3 × 2 + 4 = $10, not 3 × (2 + 4) = $18, because we multiply before we add. Java follows the same school-math habit, and adds a few more rules for its other operators.

What is operator precedence in Java?

Operator precedence is the priority of an operator. When an expression contains several operators, Java applies the ones with higher precedence first. Multiplication has higher precedence than addition, so:

public class PrecedenceBasics {
    public static void main(String[] args) {
        System.out.println(2 + 3 * 4);
        System.out.println((2 + 3) * 4);
        System.out.println(10 + 4 * 3 - 8 / 2);
        System.out.println(20 - 6 % 4);
    }
}
14
20
18
18

In the first line, 3 * 4 runs first (12), then 2 + 12. In the second line, the parentheses force the addition to happen first. The % operator has the same precedence as * and /, so 20 - 6 % 4 is 20 - 2.

How does Java evaluate an expression step by step?

Let's slow down the third line, 10 + 4 * 3 - 8 / 2. Java first handles the higher-precedence * and /, then the + and - from left to right:

Animation: 10 + 4 * 3 - 8 / 2 becomes 10 + 12 - 4, then 22 - 4, then 18
Higher precedence first (* and /), then left to right for operators on the same level (+ and -).

What is associativity in Java?

What if two operators have the same precedence, like the two / in 100 / 10 / 2? Then associativity decides. Associativity is the direction Java works in when operators are tied:

  • Left to right for almost all operators, including + - * / % and comparisons.
  • Right to left for assignment operators (= += -= …), unary operators like ! and casts, and the ternary ?:.
public class Associativity {
    public static void main(String[] args) {
        System.out.println(100 / 10 / 2);
        System.out.println(100 / (10 / 2));
        System.out.println(10 - 4 - 3);
        System.out.println(10 - (4 - 3));

        int a, b;
        a = b = 7;
        System.out.println(a + " " + b);
    }
}
5
20
3
9
7 7

100 / 10 / 2 is (100 / 10) / 2, which is 5. Assignment goes the other way: a = b = 7 means a = (b = 7), as you saw with chained assignment.

Java operator precedence table

Here are Java's operators from highest precedence (top) to lowest (bottom). Operators in the same row have equal precedence.

LevelOperatorsDescriptionAssociativity
1x++ x--postfix increment/decrementleft to right
2++x --x +x -x ! ~ (type)prefix, unary plus/minus, NOT, bitwise NOT, castright to left
3* / %multiply, divide, remainderleft to right
4+ -add, subtract, join Stringsleft to right
5<< >> >>>bit shiftsleft to right
6< > <= >= instanceofrelationalleft to right
7== !=equalityleft to right
8&bitwise / logical ANDleft to right
9^bitwise / logical XORleft to right
10|bitwise / logical ORleft to right
11&&conditional ANDleft to right
12||conditional ORleft to right
13? :ternary (conditional)right to left
14= += -= *= /= %= and othersassignmentright to left

You don't need to memorize all of it. Remember the big picture: unary → math → comparisons → logic → assignment. The ternary operator ?: gets its own lesson in Java ternary operator, and instanceof comes up with polymorphism.

💡 Tip

Parentheses beat everything. If you have to stop and think about which operator runs first, add parentheses. They cost nothing at runtime and make your intent obvious to the next reader.

Why does "Total: " + 2 + 3 print Total: 23?

This is the most common precedence surprise in Java. The + operator does two jobs: it adds numbers, and it joins (concatenates) Strings. Since all the + signs have the same precedence, Java works left to right, and as soon as one side is a String, + means "join".

public class StringSurprise {
    public static void main(String[] args) {
        int apples = 2;
        int pears = 3;

        System.out.println("Total: " + apples + pears);
        System.out.println("Total: " + (apples + pears));
        System.out.println(apples + pears + " fruits");
        System.out.println("Cost: " + apples * 3);
    }
}
Total: 23
Total: 5
5 fruits
Cost: 6
  • "Total: " + apples becomes the String "Total: 2", and then "Total: 2" + pears becomes "Total: 23".
  • With parentheses, apples + pears runs first and gives the number 5.
  • apples + pears + " fruits" starts with two numbers, so they're added (5) before the text is joined.
  • * has higher precedence than +, so apples * 3 is worked out before the join. No parentheses needed, though they wouldn't hurt.

Other common precedence surprises

The average that's way too high

public class AverageBug {
    public static void main(String[] args) {
        int quiz1 = 80;
        int quiz2 = 90;
        int quiz3 = 70;

        double wrong = quiz1 + quiz2 + quiz3 / 3;
        double right = (quiz1 + quiz2 + quiz3) / 3.0;

        System.out.println("Wrong average: " + wrong);
        System.out.println("Right average: " + right);
    }
}
Wrong average: 193.0
Right average: 80.0

Without parentheses, only quiz3 is divided by 3 (giving 23), and then everything is added up. The fix also uses 3.0 to avoid integer division.

&& binds tighter than ||

public class LogicPrecedence {
    public static void main(String[] args) {
        boolean isMember = true;
        boolean hasCoupon = false;
        boolean saleIsOn = false;

        boolean discount1 = isMember || hasCoupon && saleIsOn;
        boolean discount2 = (isMember || hasCoupon) && saleIsOn;

        System.out.println("Without parentheses: " + discount1);
        System.out.println("With parentheses:    " + discount2);

        int x = 5;
        int y = x++ * 2 + --x;
        System.out.println("y = " + y + ", x = " + x);
    }
}
Without parentheses: true
With parentheses:    false
y = 15, x = 5

Because && comes before ||, the first line means isMember || (hasCoupon && saleIsOn), which is true. If the rule is "members or coupon holders, but only during a sale", you need the parentheses from the second line. (See logical operators for a refresher.)

The last line shows why mixing ++ and -- into bigger expressions is a bad idea. Java works it out left to right: x++ gives 5 (x becomes 6), times 2 is 10, then --x makes x 5 again and gives 5, so y is 15. Correct, but nobody should have to work that hard to read one line.

A cast applies to the value right after it

public class CastPrecedence {
    public static void main(String[] args) {
        int distance = 7;
        int hours = 2;

        double speed1 = (double) distance / hours;
        double speed2 = (double) (distance / hours);

        System.out.println(speed1);
        System.out.println(speed2);
        System.out.println(-3 * -2);
        System.out.println(!true == false);
    }
}
3.5
3.0
6
true

A cast has very high precedence, so (double) distance / hours casts distance first and then divides (3.5). In speed2, the parentheses make the integer division happen first (3), and only the result is cast (3.0). Unary minus and ! are also high on the list, so -3 * -2 and !true == false work as you'd hope.

Real-world example: a checkout total

Here's a typical calculation where parentheses make the order crystal clear: subtract a discount from the item total, then add 10% tax.

public class CheckoutTotal {
    public static void main(String[] args) {
        double price = 12.0;
        int quantity = 3;
        double discount = 5.0;
        double taxRate = 0.10;

        double total = (price * quantity - discount) * (1 + taxRate);

        System.out.println("Items: " + quantity + ", total: $" + total);
    }
}
Items: 3, total: $34.1

Inside the first parentheses, price * quantity runs before - discount by normal precedence (36 - 5 = 31). Then 31 × 1.1 = 34.1. Without the parentheses, Java would compute price * quantity - discount * (1 + taxRate), which is 36 - 5.5 = 30.5.

Common mistakes with operator precedence

1. Forgetting parentheses when joining numbers to text

"Score: " + a + b joins instead of adding. Write "Score: " + (a + b).

2. Dividing only the last term

a + b + c / 3 divides just c. Write (a + b + c) / 3.0.

3. Mixing && and || without parentheses

The code compiles either way, so the bug is silent. Group the conditions you mean.

4. Casting the wrong thing

(double) (a / b) casts after integer division has already thrown the decimals away. Write (double) a / b.

Practice exercise

🏋️ Try it yourself

Without running it, predict what each line prints. Then check your answers against the solution.

public class PredictIt {
    public static void main(String[] args) {
        int a = 8;
        int b = 3;

        System.out.println(a + b * 2);
        System.out.println((a + b) * 2);
        System.out.println(a / b * b);
        System.out.println(a % b + a / b);
        System.out.println("Sum: " + a + b);
        System.out.println(a > b && b > 5 || a == 8);
    }
}
Show solution

The program prints six lines: 14, 22, 6, 4, Sum: 83 and true. Here's why:

  • a + b * 2 → 8 + 6 = 14.
  • (a + b) * 2 → 11 × 2 = 22.
  • a / b * b → left to right: 8 / 3 = 2 (integer division), then 2 × 3 = 6.
  • a % b + a / b → 2 + 2 = 4.
  • "Sum: " + a + b → joined left to right: Sum: 83.
  • Comparisons first: true && false || true. Then &&: false || true, which is true.

What's next?

You've finished the Operators chapter. You've seen Strings pop up again and again, so now it's time to study them properly. Start with Java strings.

Key takeaways

  • Operator precedence decides which operator runs first: 2 + 3 * 4 is 14 because * comes before +.
  • When operators have the same precedence, associativity decides: left to right for most, right to left for assignment, unary operators and ?:.
  • The broad order is: unary operators, then math, then comparisons, then logical operators, then assignment.
  • "Total: " + 2 + 3 prints Total: 23 because + works left to right and joins once a String is involved; use parentheses to add first.
  • && has higher precedence than ||, so a || b && c means a || (b && c).
  • Parentheses always run first; use them whenever the order is not instantly obvious.

Frequently asked questions

What is operator precedence in Java?

It is the priority order Java uses when an expression has several operators. Operators with higher precedence, such as * and /, are applied before lower ones, such as + and -.

Which operator has the highest precedence in Java?

Among the operators covered here, postfix increment and decrement (x++ and x--) have the highest precedence, followed by prefix and unary operators such as ++x, -x, ! and casts. Parentheses override everything.

What is the difference between precedence and associativity?

Precedence decides which of two different operators runs first. Associativity decides the order for operators with the same precedence, for example left to right for 100 / 10 / 2, which gives 5.

Why does "Sum: " + 1 + 2 print Sum: 12 in Java?

All the + operators have the same precedence and run left to right. "Sum: " + 1 makes the String "Sum: 1", and adding 2 joins it again. Write "Sum: " + (1 + 2) to get Sum: 3.

Should I use parentheses even when they are not needed?

Yes, when they make the code easier to read. Parentheses have no performance cost, and they make your intent clear, especially when mixing && with || or text with numbers.