Java: Operator Precedence
Each operator in Java has a set priority or precedence in relation to the others, as shown in the following table. Operators with a higher precedence are executed before those of a lower precedence, and where two or more operators are of equal precedence they are executed in a particular sequence called the associativity of the operators. Order of precedence in the table is highest at the top down to lowest at the bottom:
| Operators | Associativity |
|---|---|
| (), [], . | left to right |
| unary +, unary -, ++, --, ~, !, (type) | right to left |
| *, /, % | left to right |
| +, - | left to right |
| <<, >>, >>> | left to right |
| < ,<= , >, >=, instanceof | left to right |
| ==, != | left to right |
| & | left to right |
| ^ | left to right |
| | | left to right |
| && | left to right |
| || | left to right |
| ?: | left to right |
| =, +=, -=, *=, /=, %=, <<=, >>=, >>>=, &=, |=, ^= | right to left |
Operators that appear in the same box in the table are of equal precedence. The sequence of execution of operators with equal precedence in a statement is determined by the associativity of the group. If the associativity is left to right, they are executed in sequence starting with the leftmost and ending with the rightmost. Right to left associativity runs in the opposite direction.
