Java Reference
In-Depth Information
• If v is of type float and is an element of the float-extended-exponent value set, then
the implementation must map v to the nearest element of the float value set. This
conversion may result in overflow or underflow.
• If v is of type double and is an element of the double-extended-exponent value set,
then the implementation must map v to the nearest element of the double value set.
This conversion may result in overflow or underflow.
The only exceptions that an assignment conversion may cause are:
• A ClassCastException if, after the type conversions above have been applied, the res-
ulting value is an object which is not an instance of a subclass or subinterface of
the erasure (§ 4.6 ) of the type of the variable.
This circumstance can only arise as a result of heap pollution (§ 4.12.2 ). In
practice, implementations need only perform casts when accessing a field or
method of an object of parametized type, when the erased type of the field, or
the erased result type of the method differ from their unerased type.
• An OutOfMemoryError as a result of a boxing conversion.
• A NullPointerException as a result of an unboxing conversion on a null reference.
• An ArrayStoreException in special cases involving array elements or field access
10.5 , § 15.26.1 ) .
Example 5.2-1. Assignment Conversion for Primitive Types
Click here to view code image
class Test {
public static void main(String[] args) {
short s = 12; // narrow 12 to short
float f = s; // widen short to float
System.out.println("f=" + f);
char c = '\u0123';
long l = c; // widen char to long
System.out.println("l=0x" + Long.toString(l,16));
f = 1.23f;
double d = f; // widen float to double
System.out.println("d=" + d);
}
}
This program produces the output:
f=12.0
Search WWH ::




Custom Search