As a Java developer, you typically have to write code that includes complicated expressions that use different symbols. It’s usual for beginners to get frustrated when a line of code appears right but gives a result that is completely different from what they expected. This usually happens because of a misunderstanding of Java operator precedence. Without a clear understanding of which operation happens first, your logic can easily fail.
Beyond simple priority, you must also understand Java operator precedence and associativity. When two operators have the same level of priority, “associativity” tells the computer whether to read the expression from left to right or right to left. This article has Java operator precedence table and the rules you need to know to develop Java programs that are clean and free of bugs.
What is Java Operator Precedence?
Java Operator Precedence is a group of rules that tell you which operator to look at first. For instance, in the equation 10 + 5 * 2, the multiplication happens first since * has a greater priority than +.
You must use Parentheses () to break these restrictions because they have the highest priority in all of Java. Not only do brackets make sure the right order, but they also make your code easier for other programmers to read.
Java Operator Precedence and Associativity Table
The following chart shows the order of Java operators from highest priority (top) to lowest priority (bottom).
| Precedence | Operator Type | Operators | Associativity |
| 1 | Postfix | expr++, expr– | Left-to-right |
| 2 | Unary | ++expr, –expr, +, -, ~, ! | Right-to-left |
| 3 | Multiplicative | *, /, % | Left-to-right |
| 4 | Additive | +, – | Left-to-right |
| 5 | Shift | <<, >>, >>> | Left-to-right |
| 6 | Relational | <, >, <=, >=, instanceof | Left-to-right |
| 7 | Equality | ==, != | Left-to-right |
| 8 | Bitwise AND | & | Left-to-right |
| 9 | Bitwise XOR | ^ | Left-to-right |
| 10 | Bitwise OR | ` | ` |
| 11 | Logical AND | && | Left-to-right |
| 12 | Logical OR | ` | |
| 13 | Ternary | ? : | Right-to-left |
| 14 | Assignment | =, +=, -=, *=, /=, %= | Right-to-left |
Understanding Java Operator Precedence and Associativity
When an expression has two operators with the same level of precedence, the Java operator precedence and associativity rules decide which way to go with the calculation.
- Left-to-Right: Most operators, like addition and subtraction, follow this. In 10 – 5 – 2, it calculates (10 – 5) first, then – 2.
- Right-to-Left: Assignment and Unary operators follow this. In int x = y = 10;, Java first assigns 10 to y, and then assigns the value of y to x.
Example for Java Operator Precedence: Solving a Complex Expression
Let’s see how a Java Developer would look at this line:
boolean result = 5 + 3 * 2 > 10 && true;
5 + 6 > 10 and true, thus multiplication comes first.
11 > 10 and true come next.
Relational third: true and true
Last but not least: true
We can understand exactly how the machine gets the final answer by looking at the table of Java operator precedence and associativity.
Here are 5 more examples:
- Arithmetic precedence (* before +)
int a = 2 + 3 * 4;
Java applies multiplication first, so it becomes 2 + 12, and a ends up as 14. If you expected 20, you were mentally grouping it as (2 + 3) * 4. - Parentheses override precedence
int b = (2 + 3) * 4;
Because parentheses have the highest priority, Java evaluates (2 + 3) first, giving 5 * 4, so b = 20. Parentheses are also the cleanest way to make intent obvious. - Same precedence uses left-to-right associativity
int c = 20 / 5 * 2;
Both / and * share the same precedence and associate left-to-right, so Java does (20 / 5) first which is 4, then 4 * 2 = 8. It is not evaluated as 20 / (5 * 2). - Mixing arithmetic + comparison + logical operators
boolean ok = 5 + 3 * 2 > 10 && 8 – 3 == 5;
Java does 3 * 2 first, then 5 + 6, then compares 11 > 10. Separately, it evaluates 8 – 3 then checks equality. Finally, it applies && to combine both boolean results. - Precedence vs operand evaluation order (important in real code)
int r = f() + g() * h(i(), j());
Precedence groups it like f() + (g() * h(i(), j())), but operands/arguments are evaluated left-to-right: f(), then g(), then i(), then j(), then h(…), and only then the multiplication and addition are applied.
How to Apply Java Operator Precedence in Real Code
Java operator precedence tells Java how to group operators and operands, but it does not control the order in which operands are evaluated. In Java, operands are evaluated left-to-right, including method arguments. So in an expression like A() + B() * C(D(), E()), Java evaluates A(), then B(), then D(), then E(), and only then calls C(…) even though * has higher precedence than +.
Many developers use parentheses for readability, not just correctness. For example, (2 + 3) * 4 forces addition first, while 10 – 2 + 5 naturally evaluates left-to-right unless you group it differently.
Also, avoid “looks-mathy” shortcuts that Java doesn’t allow, like x <= y <= z or stacking post-operators like x++–. These are not valid in Java, so please write comparisons explicitly.
Java Operator Precedence (FAQs)
1. What is the highest precedence operator in Java?
The most priority is given to brackets (). They are used to group expressions and make sure that they are evaluated in a certain sequence, even if the normal Java operator precedence table says otherwise.
2. What are the differences between associativity and precedence?
Precedence determines the order in which distinct operators are solved (for example, * before +). When the same operators or operators with the same priority are together, associativity decides the order (for example, 10 / 5 * 2).
3. Do math operators usually come before logical ones?
Yes, in the Java operator precedence table, arithmetic operators (such as multiplication, division, and addition) come before relational (>) and logical (&&, ||) operators.
4. Why does assignment associativity go from right to left?
This lets you do “chained assignments,” like a = b = c = 50. The number 50 is sent from right to left, making sure that all variables have the same value.
5. Where can I get a Java operator precedence chart?
For developers, the official Oracle documentation and the Java operator precedence and associativity table in this tutorial are the best sources.
Read More Java Topics
🔹 Java Introduction & Fundamentals |
🔹 Java Basics & Syntax |
🔹 OOPS Concepts |
🔹 Collections & DSA |
🔹 Exception & Multithreading |
🔹 JVM & Architecture |
🔹 Frameworks & Backend |
🔹 Java Programs & Practice |
🔹 Java Career & Jobs |
🔹 Other / Unclassified Java Topics |
| 502 Bad Gateway |
| 502 Bad Gateway |
