Java Reference
In-Depth Information
showing that you can cast from integral types to floating point types and back.
The following diagram shows one of the narrower-wider hierarchies begin-
ning with byte . A type is narrower than the types to its right and wider than the
types to its left.
byte short int long float double
A second diagram shows how type char enters into the picture.
char int long float double
An expression of one type may be promoted to a wider type automatically if
it appears in a context where the wider type is required. But all narrowing con-
versions must be given explicitly using a cast. A cast, as explained in Sec. 6.3, is
a prefix operator of the form ( type ) . Examples are ( double ) and ( char ) .
Casting to a narrower type may result in garbage if the value being cast is
not in the range of the type to which it is being cast.
6.7
Type boolean
In Java, primitive type boolean describes the set of two values false and true
(called literals), together with the following operations on them: negation ! ; con-
junction (and) && ; and disjunction (or) || . The word boolean comes from the
name of George Boole, a famous mathematician in the 1800s who founded the
area known as logic .
A boolean expression is an expression whose evaluation produces either
false or true . For example, arithmetic relations like x<y and b!=c are
boolean expressions. If int variables x and y contain 6 and 4 , respectively, then
x<y evaluates to false because 6 is not less than 4 .
Boolean expressions are used as the conditions of if-statements and loops.
However, one can also assign boolean expressions to variables, as in:
Lesson page
6.6 contains a
complete
description of
type boolean .
boolean isLess;
isLess= x < y;
Boolean operators
We now discuss five boolean operators. We use variables b and c as their
b
c
|
!b
b && c
b || c
b == c
b != c
--------------------------------------------
tt|f
t
t
t
f
tf|f
f
t
f
t
ft|t
f
t
f
t
ff|t
f
f
t
f
Figure 6.2:
Defining boolean operators in a truth table. t and f represent true and false
Search WWH ::




Custom Search