Within a single expression, opertions with larger precedence numbers (as shown below) are perfomed below those with smaller numbers; operations at the same level are performed in order of association.
Style tip: when writing expressions that use operators having different precedence levels, the use of parentheses is encouraged. Even if they are, technically, not needed, their use makes the code clearer and less prone to subtle errors (in syntax, semantics, or logic).
Category | Operator | Precedence | Association |
---|---|---|---|
subexpression | ( ) | 11 | left to right |
(If parentheses are nested, |
|||
postincrement postdecrement dot |
++ -- . |
10 | left to right |
preincrement predecrement unary operators |
++ -- ! - |
9 | right to left |
type cast object instantiation |
(type) new |
8 | right to left |
multiplicative operators | * / % |
7 | left to right |
additive operators string concatenation |
+ - + |
6 | left to right |
relational operators | < <= > >= |
5 | left to right |
equality operators | == != |
4 | left to right |
boolean XOR | ^ | 3 | left to right |
boolean AND | && | 2 | left to right |
boolean OR | || | 1 | left to right |
assignment operators | = |
0 | right to left |