Java Reference
In-Depth Information
T ABLE 3.8
Operator Precedence Chart
Precedence
Operator
var++ and var-- (Postfix)
+ , - (Unary plus and minus), ++var and --var (Prefix)
(type) (Casting)
! (Not)
* , / , % (Multiplication, division, and remainder)
+ , - (Binary addition and subtraction)
< , <= , > , >= (Relational)
== , != (Equality)
^ (Exclusive OR)
&& (AND)
|| (OR)
= , += , -= , *= , /= , %= (Assignment operator)
If operators with the same precedence are next to each other, their associativity determines
the order of evaluation. All binary operators except assignment operators are left associative .
For example, since + and - are of the same precedence and are left associative, the expression
operator associativity
is equivalent to
a - b + c - d
((a - b) + c) - d
Assignment operators are right associative . Therefore, the expression
is equivalent to
a = b += c = 5
a = (b += (c = 5))
Suppose a , b , and c are 1 before the assignment; after the whole expression is evaluated, a
becomes 6 , b becomes 6 , and c becomes 5 . Note that left associativity for the assignment
operator would not make sense.
Note
Java has its own way to evaluate an expression internally. The result of a Java evaluation
is the same as that of its corresponding arithmetic evaluation. Advanced readers may
refer to Supplement III.B for more discussions on how an expression is evaluated in Java
behind the scenes .
behind the scenes
3.37
List the precedence order of the Boolean operators. Evaluate the following expressions:
Check
Point
true || true && false
true && true || false
3.38
True or false? All the binary operators except = are left associative.
3.39
Evaluate the following expressions:
2 * 2 - 3 > 2 && 4 - 2 > 5
2 * 2 - 3 > 2 || 4 - 2 > 5
 
 
Search WWH ::




Custom Search