Describe how operator precedence and associativity is used to determine the order in which expressions are evaluated.
The
order of evaluation of Java expressions is dependent on the
precedence and associativity of the operators used in the expression, and the use of parentheses.
In general, the operands of expressions are evaluated in a left-to-right order before any operations are performed. The exceptions to this are expressions involving the
&&
,
||
, and ternary operators.
Once an expression's operands are evaluated, the operators are applied according to operator precedence and associativity.
The assignment operator is the only right-associative binary operator. Parentheses can be used to change the ordering imposed by precedence and associativity.
The following table shows the precedence of Java's operators. Higher-level operators take precedence over lower-level operators.
Operators at the same level are evaluated based on the order they appear and their associativity.
- Order of evaluation: The order in which expressions are evaluated.
- Precedence: The order in which operations are performed.