Java Reference
In-Depth Information
the operator
||
evaluates before the operator
&&
; whereas, in the expression:
7 >= 8 || 'A' < 'B' && 5 * 4 == 20
the operator
&&
evaluates before the operator
||
.
Example 4-5 illustrates how logical expressions consisting of variables are evaluated.
EXAMPLE 4-5
Suppose you have the following declarations:
4
boolean
found =
true
;
double
hours = 45.30;
double
overTime = 15.00;
int
count = 20;
char
ch = 'B';
Consider the following expressions:
Expression
Value / Explanation
!found
false
Because found is
true
, !found is
false
.
hours > 40.00
true
Because hours is 45.30 and 45.30 > 40.00 is
true
, the expression hours > 40.00 evaluates to
true
.
!found && (hours >= 0)
false
!found is
false
; hours >= 0 is 45.30 >= 0 is
true
. Therefore,!found && (hours >= 0) is
false
&&
true
, which evaluates to
false
.
!(found && (hours >= 0))
false
Now, found && (hours >= 0) is
true
&&
true
,
which evaluates to
true
.Therefore,!(found &&
(hours >= 0)) is !
true
, which evaluates to
false
.
hours + overTime <= 75.00
true
Because hours + overTime is 45.30 + 15.00
= 60.30 and 60.30 <= 75.00 is
true
, it follows
that hours + overTime <= 75.00 evaluates to
true
.
(count >= 0) &&
(count <= 100)
true
Now count is 20. Because 20 >= 0 is
true
,
count >= 0 is
true
. Also, 20 <= 100 is
true
,
so count <= 100 is
true
. Therefore,
(count >= 0) && (count <= 100) is
true
&&
true
, which evaluates to
true
.
('A' <= ch && ch <= 'Z')
true
Here, ch is 'B'. Because 'A' <= 'B' is
true
,
'A' <= ch evaluates to
true
. Also, because 'B'
<= 'Z' is
true
, ch <= 'Z' evaluates to
true
.
Therefore, ('A' <= ch && ch <= 'Z') is
true
&&
true
, which evaluates to
true
.
Search WWH ::
Custom Search