Main Page

Previous Next

Operator Precedence

We have already introduced the idea of a pecking order for operators, which determines the sequence in which they are executed in a statement. A simple arithmetic expression such as 3 + 4*5 results in the value 23 because the multiply operation is executed first – it takes precedence over the addition operation. We can now formalize the position by classifying all the operators present in Java. 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. Precedence is highest for operators in the top line in the table, down through to the operators in the bottom line, which have the lowest precedence:

Operator Precedence Group

Associativity

(), [], . postfix ++, postfix --

left

unary +, unary -, prefix ++, prefix --, ~, !

right

(type), new

left

*, /, %

left

+, -

left

<<, >>, >>>

left

< ,<= , >, >=, instanceof

left

==, !=

left

&

left

^

left

|

left

&&

left

||

left

?:

left

=, +=, -=, *=, /=, %=, <<=, >>=, >>>=, &=, |=, ^=

right

Note 

Most of the operators that appear in the table you have not seen yet, but you will meet them all in this book eventually, and it is handy to have them all gathered together in a single precedence table that you can refer to when necessary.

By definition, the postfix ++ operator is executed after the other operators in the expression in which it appears, despite its high precedence. In this case, precedence determines what it applies to, in other words, the postfix ++ only acts on the variable that appears immediately before it. For this reason the expression oranges+++apples that we saw earlier is evaluated as (oranges++) + apples rather than oranges + (++apples).

The sequence of execution of operators with equal precedence in a statement is determined by a property called associativity. Each group of operators appearing on the same line in the table above are either left associative or right associative. A left associative operator attaches to its immediate left operand. This results in an expression involving several left associative operators with the same precedence in the same expression being executed in sequence starting with the leftmost and ending with the rightmost. Right associative operators of equal precedence in an expression bind to their right operand and consequently are executed from right to left. For example, if you write the statement:

a = b + c + 10;

the left associativity of the group to which the + operator belongs implies that this is effectively:

a = (b + c) + 10;

On the other hand, = and op= are right associative, so if you have int variables a, b, c, and d each initialized to 1, the statement:

a += b = c += d = 10;

sets a to 12, b and c to 11, and d to 10. The statement is equivalent to:

a += (b = (c += (d = 10)));
Note 

Note that these statements are intended to illustrate how associativity works, and are not a recommended approach to coding.

You will probably find that you will learn the precedence and associativity of the operators in Java by just using them in your programs. You may need to refer back to the table from time to time, but as you gain experience you will gain a feel for where the operators sit and eventually you will automatically know when you need parentheses and when not.

Previous Next
JavaScript Editor Java Tutorials Free JavaScript Editor