Java Reference
In-Depth Information
Assignment Compatibilities
You can assign a value of any type on the following list to a variable of any type that appears
further down on the list:
byte
—>
short
—>
int
—>
long
—>
float
—>
double
In particular, note that you can assign a value of any integer type to a variable of any floating-
point type. You can also assign a value of type
char
to a variable of type
int
or of any type
that follows
int
in the above list.
are written in the way you are used to writing numbers. Constants of type
int
(or any
other integer type) must not contain a decimal point. Constants of floating-point types
(
float
and
double
) may be written in either of two forms. The simple form for floating-
point constants is like the everyday way of writing decimal fractions. When written in
this form, a floating-point constant must contain a decimal point. No number constant
(neither integer nor floating point) in Java may contain a comma.
A more complicated notation for floating-point constants, such as constants of type
double
, is called
scientific notation
or
floating-point notation
and is particularly
handy for writing very large numbers and very small fractions. For instance,
e
notation
3.67
x
10
5
, which is the same as
367000.0
,
is best expressed in Java by the constant
3.67e5
. The number
5.89
x
10
-4
, which is the same as
0.000589
,
is best expressed in Java by the constant
5.89e
−
4
. The
e
stands for
exponent
and means
“multiply by 10 to the power that follows.” The
e
may be either uppercase or lowercase.
Think of the number after the
e
as telling you the direction and number of digits to
move the decimal point. For example, to change
3.49e4
to a numeral without an
e
,
you move the decimal point
4
places to the right to obtain
34900.0
, which is another
way of writing the same number. If the number after the
e
is negative, you move the
decimal point the indicated number of spaces to the left, inserting extra zeros if need
be. So,
3.49e
−
2
is the same as
0.0349
.
The number before the
e
may contain a decimal point, although that is not required.
However, the exponent after the
e
definitely must
not
contain a decimal point.
Constants of type
char
are expressed by placing the character in single quotes, as
illustrated in what follows:
char
symbol = 'Z';
Note that the left and right single quote symbol are the same symbol.