Java Reference
In-Depth Information
Table 4-7 defines the operator
||
(or). From this table, it follows that
Expression1 ||
Expression2
is
true
if and only if at least one of the expressions,
Expression1
or
Expression2
,is
true
;otherwise,
Expression1 || Expression2
evaluates to
false
.Table
4-7 is called the truth table of the operator
||
. Example 4-3 gives examples of the
||
operator.
TABLE 4-7
||
(or) Operator
Expression1
Expression2
Expression1
||
Expression2
4
true
true
true
true
false
true
false
true
true
false
false
false
EXAMPLE 4-3
Expression
Value
Explanation
true
Because
(14 >= 5)
is
true
,
('A' > 'B')
is
false
, and
true
||
false
is
true
, the
expression evaluates to
true
.
(14 >= 5) || ('A' > 'B')
false
Because
(24 >= 35)
is
false
,
('A' > 'B')
is
false
,and
false
||
false
is
false
,the
expression evaluates to
false
.
(24 >= 35) || ('A' > 'B')
('A' <= 'a') || (7 != 7)
true
Because
('A' <= 'a')
is
true
,
(7 != 7)
is
false
, and
true
||
false
is
true
, the
expression evaluates to
true
.
To work with complex logical expressions, there must be some priority scheme for
determining which operators to evaluate first. Because an expression might contain
arithmetic, relational, and logical operators, as in the expression
5 + 3 <= 9 && 2 > 3
,
an order of precedence for the Java operators must be established. Table 4-8 shows the
order of precedence of some Java operators, including the arithmetic, relational, and
logical operators. (See Appendix B for the precedence of all Java operators.)
Search WWH ::
Custom Search