Java Reference
In-Depth Information
If S is an intersection type A 1 & ... & A n , then it is a compile-time error if there exists an A i
(1 ≤ i n ) such that S cannot be cast to A i by this algorithm. That is, the success of the cast
is determined by the most restrictive component of the intersection type.
If S is an array type SC [] , that is, an array of components of type SC :
• If T is a class type, then if T is not Object , then a compile-time error occurs (because
Object is the only class type to which arrays can be assigned).
• If T is an interface type, then a compile-time error occurs unless T is the type
java.io.Serializable or the type Cloneable (the only interfaces implemented by arrays).
• If T is a type variable, then:
♦ If the upper bound of T is Object or java.io.Serializable or Cloneable , or a type vari-
able that S could undergo casting conversion to, then the cast is legal (though
unchecked).
♦ If the upper bound of T is an array type TC [] , then a compile-time error occurs
unless the type SC [] can undergo casting conversion to TC [] .
♦ Otherwise, a compile-time error occurs.
• If T is an array type TC [] , that is, an array of components of type TC , then a
compile-time error occurs unless one of the following is true:
TC and SC are the same primitive type.
TC and SC are reference types and type SC can undergo casting conversion to
TC .
Example 5.5.1-1. Casting Conversion for Reference Types
Click here to view code image
class Point { int x, y; }
interface Colorable { void setColor(int color); }
class ColoredPoint extends Point implements Colorable {
int color;
public void setColor(int color) { this.color = color; }
}
final class EndPoint extends Point {}
class Test {
public static void main(String[] args) {
Point p = new Point();
ColoredPoint cp = new ColoredPoint();
Colorable c;
// The following may cause errors at run time because
Search WWH ::




Custom Search