Java Reference
In-Depth Information
Operator
Meaning
==
!=
<
<=
>
>=
equal to
not equal to
less than
less than or equal to
greater than
greater than or equal to
FIGURE 5.1
Java equality and relational operators
The equality and relational operators have precedence lower than the arith-
metic operators. Therefore, arithmetic operations are evaluated first, followed by
equality and relational operations. As always, parentheses can be used to explic-
itly specify the order of evaluation.
We'll see more examples of relational operators as we examine conditional and
loop statements throughout this chapter.
In addition to the equality and relational operators, Java has three
logical opera-
tors
that produce boolean results. They also take boolean operands. Figure 5.2
lists and describes the logical operators.
The
!
operator is used to perform the
logical NOT
operation, which is also
called the
logical complement.
The logical complement of a boolean value yields
its opposite value. That is, if a boolean variable called
found
has the value false,
then
!found
is true. Likewise, if
found
is true, then
!found
is false. The logical
NOT operation does not change the value stored in
found
.
A logical operation can be described by a
truth table
that lists all possible com-
binations of values for the variables involved in an expression. Because the logical
Operator
Description
Example
Result
!
&&
||
! a
a && b
a || b
true if
a
is false and false if
a
is true
true if
a
and
b
are both true and false otherwise
true if
a
or
b
or both are true and false otherwise
logical NOT
logical AND
logical OR
FIGURE 5.2
Java logical operators
Search WWH ::
Custom Search