Java Reference
In-Depth Information
TABLE 4-8 Precedence of Operators
Operators
Precedence
! , + , - (unary operators)
first (highest)
* , / , %
second
+ , -
third
< , <= , >= , >
fourth
== , !=
fifth
&&
sixth
||
seventh
= (assignment operator)
last (lowest)
Using the precedence rules given in Table 4-8, in an expression, relational and logical
operators are evaluated from left to right, and consequently the associativity of these
operators is said to be from left to right.
You can insert parentheses into an expression to clarify its meaning or to affect the precedence.
EXAMPLE 4-4
Evaluate the following expression:
(17 < 4 * 3 + 5) || (8 * 2 ¼¼ 4 * 4) && !(3 + 3 ¼¼ 6)
Now:
(17 < 4 * 3 + 5) || (8 * 2 == 4 * 4) && !(3 + 3 == 6)
=
(17 < 12 + 5) || (16 == 16) && !(6 == 6)
=
(17 < 17) || true && !( true )
=
false || true && false
=
false || false (because true && false is false )
=
false
Therefore, the value of the original logical expression is false .
You can also use parentheses to override the precedence of operators. For example, in the
expression:
(7 >= 8 || 'A' < 'B') && 5 * 4 == 20
Search WWH ::




Custom Search