Java Reference
In-Depth Information
• Conjunction, or
and
:
b1 && b2
Expression
b1 && b2
evaluates to
true
if both
b1
and
b2
are
true
and
evaluates to
false
otherwise. Operands
b1
and
b2
are called
conjuncts
.
• Disjunction, or
or
:
b1 || b2
Expression
b1 || b2
evaluates to
true
if either
b1
or
b2
(or both) is
true
and to
false
otherwise. Operands
b1
and
b2
are called
disjuncts
.
For example, we evaluate the expression
true
&& !(
false
||
false
)
:
1.
false
||
false
evaluates to
false
,
so the expression becomes
true
&& !
false
.
2.
!false
evaluates to
true
,
so the expression becomes
true
&&
true
.
3.
true
&&
true
evaluates to
true
.
You are not likely to use operations
!
,
&&
, and
||
very much in the begin-
ning. However, we need type
boolean
in order to talk about arithmetic
relations
,
which yield
boolean
values. Assuming that
e1
and
e2
are both
int
or
double
expressions, we can use the following arithmetic relations:
e1 < e2
true
if
e1
is less than
e2
and
false
otherwise.
e1 > e2
true
if
e1
is greater than
e2
and
false
otherwise.
e1 <= e2
true
if
e1
is at most
e2
and
false
otherwise.
e1 >= e2
true
if
e1
is at least
e2
and
false
otherwise.
e1 == e2
true
if
e1
and
e2
are equal and
false
otherwise.
e1 != e2
true
if
e1
and
e2
are different and
false
otherwise.
For example, the expression
5+3<9
evaluates to
true
, and
5+3<8
eval-
uates to
false
. The operators
<
,
>
,
<=
,
>=
,
==
, and
!=
are called
relational oper-
ators
.
Be careful with equality: The phrase
5=6
is not a Java expression! Java uses
the sign
==
for an equality test, rather than
=
, and uses
=
in another way, which
we describe in Sec. 1.2. For example,
6==6
evaluates to
true
and
5==6
eval-
uates to
false
. This breaking of mathematical convention is unfortunate. For
hundreds of years, ever since Robert Recorde introduced the sign = for equality
in the 1600s, that convention has been in use. To have programming languages
break with that tradition is a travesty.
Operator
Operation
Example
Result
not
, or
negation
!
!
true
false
and
, or
conjunction
&&
true
&&
false
false
or
, or
disjunction
||
true
||
false
true
Figure 1.2:
Basic boolean operators in Java
Search WWH ::
Custom Search