Java Reference
In-Depth Information
>=
Greater than or equal to
<=
Less than or equal to
The logical operators are shown next:
Operator
Meaning
&
AND
|
OR
^
XOR (exclusive OR)
||
Short-circuit OR
&&
Short-circuit AND
!
NOT
The outcome of the relational and logical operators is a
boolean
value.
In Java, all objects can be compared for equality or inequality using = = and
!=
.
However, the comparison operators,
<, >
,
<=
, or
>=
, can be applied only to those types that
support an ordering relationship. Therefore, all of the relational operators can be applied to
all numeric types and to type
char
. However, values of type
boolean
can only be compared
for equality or inequality, since the
true
and
false
values are not ordered. For example,
true
> false
has no meaning in Java.
For the logical operators, the operands must be of type
boolean
, and the result of a lo-
gical operation is of type
boolean
. The logical operators,
&
,
|
,
^
, and
!
, support the basic
logical operations AND, OR, XOR, and NOT, according to the following truth table:
As the table shows, the outcome of an exclusive OR operation is true when exactly one
and only one operand is true.
Here is a program that demonstrates several of the relational and logical operators: