Java Reference

In-Depth Information

Because the Java floating-point types can handle overflow to infinity and underflow

to zero and have a special NaN value, floating-point arithmetic never throws excep‐

tions, even when performing illegal operations, like dividing zero by zero or taking

the square root of a negative number.

The
float
and
double
primitive types have corresponding classes, named
Float

and
Double
. Each of these classes defines the following useful constants:
MIN_VALUE
,

MAX_VALUE
,
NEGATIVE_INFINITY
,
POSITIVE_INFINITY
, and
NaN
.

The infinite floating-point values behave as you would expect. Adding or subtract‐

ing any finite value to or from infinity, for example, yields infinity. Negative zero

behaves almost identically to positive zero, and, in fact, the
==
equality operator

reports that negative zero is equal to positive zero. One way to distinguish negative

zero from positive, or regular, zero is to divide by it: 1.0/0.0 yields positive infinity,

but 1.0 divided by negative zero yields negative infinity. Finally, because NaN is Not-

a-number, the
==
operator says that it is not equal to any other number, including

itself ! To check whether a
float
or
double
value is NaN, you must use the

Float.isNaN()
and
Double.isNaN()
methods.

Primitive Type Conversions

Java allows conversions between integer values and floating-point values. In addi‐

tion, because every character corresponds to a number in the Unicode encoding,

char
values can be converted to and from the integer and floating-point types. In

fact,
boolean
is the only primitive type that cannot be converted to or from another

primitive type in Java.

There are two basic types of conversions. A
widening conversion
occurs when a

value of one type is converted to a wider type—one that has a larger range of legal

values. For example, Java performs widening conversions automatically when you

assign an
int
literal to a
double
variable or a
char
literal to an
int
variable.

Narrowing conversions are another matter, however. A
narrowing conversion
occurs

when a value is converted to a type that is not wider than it is. Narrowing conver‐

sions are not always safe: it is reasonable to convert the integer value 13 to a
byte
,

for example, but it is not reasonable to convert 13,000 to a
byte
, because
byte
can

hold only numbers between -128 and 127. Because you can lose data in a narrowing

conversion, the Java compiler complains when you attempt any narrowing conver‐

sion, even if the value being converted would in fact fit in the narrower range of the

specified type:

int
i
=
13
;

byte
b
=
i
;
// The compiler does not allow this

The one exception to this rule is that you can assign an integer literal (an
int
value)

to a
byte
or
short
variable if the literal falls within the range of the variable.

If you need to perform a narrowing conversion and are confident you can do so

without losing data or precision, you can force Java to perform the conversion using