Java Reference
In-Depth Information
Consider the following expression:
8 < '5'
You might think that 8 is being compared with 5 . This is not the case. Here, the
integer 8 is being compared with the character 5 . That is, 8 is being compared with
the Unicode collating sequence of '5' , which is 53 . The Java system uses implicit
type conversion, changes '5' to 53 , and compares 8 with 53 . Therefore, the expression
8 < '5' always evaluates to true . However, the expression 8 < 5 always evaluates to
false . Note that char and int are of integral type and using explicit or implicit type
conversion, values of char type can be converted to int type and vice versa.
4
Expressions such as 4 < 6 and 'R' > 'T' are examples of logical (boolean) expressions.
When Java evaluates a logical expression, it returns the boolean value true if the logical
expression evaluates to true ; it returns the boolean value false otherwise.
Logical (Boolean) Operators and Logical
Expressions
This section describes how to form and evaluate logical expressions that are combina-
tions of other logical expressions. Logical (Boolean) operators enable you to
combine logical expressions. Java has three logical (boolean) operators, as shown in
Table 4-4.
TABLE 4-4 Logical (Boolean) Operators in Java
Operator
Description
!
not
&&
and
||
or
Logical operators take only logical values as operands and yield only logical values as
results. The operator ! is unary, so it has only one operand. The operators && and || are
binary.
Table 4-5 shows that when you use the ! operator, ! true is false and ! false is
true . Putting ! in front of a logical expression reverses the value of that logical expression.
Table 4-5 is called the truth table of the operator ! . Example 4-1 gives examples of
the ! operator.
 
 
Search WWH ::




Custom Search