THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1
optionally followed by a widening reference conversion; or an unboxing conversion optionally
followed by a widening primitive conversion.

If the resulting expression is of type byte, char, short, or int, and is a constant expression, then
a narrowing primitive conversion can be applied if the variable is of type byte, short, or char and
the value will fit in that typefor example, the assignment of the int literal 27 to a short variable
that we saw previously. If the type of the variable is Byte, Short, or Character, then after the
narrowing primitive conversion a boxing conversion can be applied:

Short s1 = 27; // implicit int to short to Short

Method invocation This occurs when the type of an expression being passed as an argument to a
method invocation is checked. Basically, the same conversions apply here as they do for assignment,
with the exception that the narrowing primitive conversions are not applied. This means, for example,
that a method expecting a short parameter will not accept the argument 27 because it is of type
intan explicit cast must be used. This restriction makes it easier to determine which method to
invoke when overloaded forms of it existfor example, if you try to pass a short to a method that
could take an int or a byte, should the short get widened to an int, or narrowed to a byte?


Numeric promotion Numeric promotion, as discussed in Section 9.1 on page 201, ensures that all the
operands of an arithmetic expression are of the appropriate type by performing widening primitive
conversions, preceded if necessary by unboxing conversions.


Casts Casts potentially allow for any of the conversions, but may fail at runtimethis is discussed in the
next section.


String conversions String conversions occur when the string concatenation operation is used (see page
214) and are discussed further in "String Conversions" on page 220.


9.4.2. Explicit Type Casts


When one type cannot be assigned to another type with implicit conversion, often it can be explicitly cast to
the other typeusually to perform a narrowing conversion. A cast requests a new value of a new type that is the
best available representation of the old value in the old type. Some casts are not allowedfor example, a
boolean cannot be cast to an intbut explicit casting can be used to assign a double to a long, as in this
code:


double d = 7.99;
long l = (long) d;


When a floating-point value is cast to an integer, the fractional part is lost by rounding toward zero; for
instance, (int)-72.3 is -72. Methods available in the Math and StrictMath classes round
floating-point values to integers in other ways. See "Math and StrictMath" on page 657 for details. A
floating-point NaN becomes the integer zero when cast. Values that are too large, or too small, to be
represented as an integer become MAX_VALUE or MIN_VALUE for the types int and long. For casts to
byte, short, or char, the floating-point value is first converted to an int or long (depending on its
magnitude), and then to the smaller integer type by chopping off the upper bits as described below.


A double can also be explicitly cast to a float, or an integer type can be explicitly cast to a smaller integer
type. When you cast from a double to a float, three things can go wrong: you can lose precision, you can
get a zero, or you can get an infinity where you originally had a finite value outside the range of a float.


Integer types are converted by chopping off the upper bits. If the value in the larger integer fits in the smaller
type to which it is cast, no harm is done. But if the larger integer has a value outside the range of the smaller

Free download pdf