, ,

Precedence and Associativity Rules for Operators – Basic Elements, Primitive Data Types, and Operators

2.5 Precedence and Associativity Rules for Operators

Precedence and associativity rules are necessary for deterministic evaluation of expressions. The operators are summarized in Table 2.18. The majority of them are discussed in subsequent sections in this chapter. See also the index entries for these operators.

Table 2.18 Operator Summary

Array element access, member access, method invocation[expression] . (args)
Unary postfix operatorsexpression ++ expression–
Unary prefix operators~ ! ++expression –expression +expression -expression
Unary prefix creation and castnew (type)
Multiplicative* / %
Additive+ –
Shift<< >> >>>
Relational< <= > >= instanceof
Equality== !=
Bitwise/logical AND&
Bitwise/logical XOR^
Bitwise/logical OR|
Conditional AND&&
Conditional OR||
Conditional?:
Arrow operator->
Assignment= += -= *= /= %= <<= >>= >>>= &= ^= |=

The following remarks apply to Table 2.18:

  • The operators are shown with decreasing precedence from the top of the table.
  • Operators within the same row have the same precedence.
  • Parentheses, (), can be used to override precedence and associativity.
  • The unary operators, which require one operand, include the following: the postfix increment (++) and decrement (–) operators from the first row, all the prefix operators (+, -, ++, –, ~, !) in the second row, and the prefix operators (object creation operator new, cast operator (type)) in the third row.
  • The conditional operator (? ๐Ÿ™‚ is ternaryโ€”that is, it requires three operands.
  • All operators not identified previously as unary or ternary are binaryโ€”that is, they require two operands.
  • All binary operators, except for the relational and assignment operators, associate from left to right. The relational operators are nonassociative.
  • Except for unary postfix increment and decrement operators, all unary operators, all assignment operators, and the ternary conditional operator associate from right to left.

Depending on the context, brackets ([]), parentheses (()), the colon (:), and the dot operator (.) can also be interpreted as separators (p. 32). See the index entries for these separators for more details.

Precedence rules are used to determine which operator should be applied first if there are two operators with a different precedence, and these operators follow each other in the expression. In such a case, the operator with the highest precedence is applied first.

The expression 2 + 3 * 4 is evaluated as 2 + (3 * 4) (with the result 14) since * has higher precedence than +.

Associativity rules are used to determine which operator should be applied first if there are two operators with the same precedence, and these operators follow each other in the expression.

Left associativity implies grouping from left to right: The expression 7 – 4 + 2 is interpreted as ((7 – 4) + 2), since the binary operators + and – both have the same precedence and left associativity.

Right associativity implies grouping from right to left: The expression – – 4 is interpreted as (- (- 4)) (with the result 4), since the unary operator – has right associativity.

The precedence and associativity rules together determine the evaluation order of the operators.

Related Posts