Operator Precedence in Java
✅ Operator Precedence in Java
Operator precedence determines the order in which operators are evaluated in expressions. Operators with higher precedence are evaluated before operators with lower precedence. If operators have the same precedence, their associativity (left-to-right or right-to-left) determines the order.
🔹 Table of Java Operator Precedence and Associativity
Precedence Level | Operators | Description | Associativity |
---|---|---|---|
1 (Highest) | () | Parentheses (grouping) | Left to right |
[] , . , obj.method() , a[i] , x.y | Array/member access, method call | Left to right | |
2 | ++ , -- , + (unary), - (unary), ~ , ! | Unary operators | Right to left |
3 | * , / , % | Multiplicative | Left to right |
4 | + , - | Additive | Left to right |
5 | << , >> , >>> | Bitwise shift | Left to right |
6 | < , <= , > , >= , instanceof | Relational | 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 | ` | ` | |
13 | ? : | Ternary conditional | Right to left |
14 | = , += , -= , *= , /= , %= | Assignment and compound assignment | Right to left |
&= , ` | =, ^=, <<=, >>=, >>>=` | ||
15 (Lowest) | -> , :: | Lambda expressions, method reference |
🔹 Example Illustrating Precedence
👉 Multiplication (*
) has higher precedence than addition (+
), so it's done first.
🔹 Using Parentheses for Clarity
To override precedence and make expressions easier to read, use parentheses:
🔹 Logical Precedence Example
Comments
Post a Comment