Java Reference
InDepth Information
123.45
0.0
.
01
Floatingpoint literals can also use exponential, or scientific, notation, in which a
number is followed by the letter
e
or
E
(for exponent) and another number. This
second number represents the power of 10 by which the first number is multiplied.
For example:
a
x
1.2345E02
// 1.2345 * 10^2 or 123.45
1
e

6
// 1 * 10^6 or 0.000001
6.02e23
// Avogadro's Number: 6.02 * 10^23
Floatingpoint literals are
double
values by default. To include a
float
value literally
in a program, follow the number with
f
or
F
:
double
d
=
6.02E23
;
float
f
=
6.02e23f
;
Floatingpoint literals cannot be expressed in hexadecimal, binary, or octal notation.
FloatingPoint Representations
Most real numbers, by their very nature, cannot be represented exactly in any finite
number of bits. Thus, it is important to remember that
float
and
double
values are
only approximations of the numbers they are meant to represent. A
float
is a 32bit
approximation, which results in at least six significant decimal digits, and a
double
is a 64bit approximation, which results in at least 15 significant digits. In
Chapter 9
,
we will cover floatingpoint representations in more detail.
In addition to representing ordinary numbers, the
float
and
double
types can also
represent four special values: positive and negative infinity, zero, and NaN. The
infinity values result when a floatingpoint computation produces a value that
overflows the representable range of a
float
or
double
. When a floatingpoint com‐
putation underflows the representable range of a
float
or a
double
, a zero value
results.
The Java floatingpoint types make a distinction between positive zero and negative
zero, depending on the direction from which the underflow occurred. In practice,
positive and negative zero behave pretty much the same. Finally, the last special
floatingpoint value is NaN, which stands for “Notanumber.” The NaN value
results when an illegal floatingpoint operation, such as 0.0/0.0, is performed. Here
are examples of statements that result in these special values:
double
inf
=
1.0
/
0.0
;
// Infinity
double
neginf
=

1.0
/
0.0
;
// Negative Infinity
double
negzero
=

1.0
/
inf
;
// Negative zero
double
NaN
=
0.0
/
0.0
;
// Notanumber