10.12.2012 Views

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CONVERSIONS AND PROMOTIONS Casting Conversion 5.5<br />

❖ If S is a final class (§8.1.1), then S must implement T, or a compiletime<br />

error occurs.<br />

◆ If T is a type variable, then this algorithm is applied recursively, using the<br />

upper bound of T in place of T.<br />

◆ If T is an array type, then S must be the class Object, or a compile-time<br />

error occurs.<br />

• If S is an interface type:<br />

◆ If T is an array type, then T must implement S, or a compile-time error<br />

occurs.<br />

◆ If T is a type that is not final (§8.1.1), then if there exists a supertype X of<br />

T, and a supertype Y of S, such that both X and Y are provably distinct parameterized<br />

types, and that the erasures of X and Y are the same, a compile-time<br />

error occurs. Otherwise, the cast is always legal at compile time (because<br />

even if T does not implement S, a subclass of T might).<br />

◆ If T is a type that is final, then:<br />

❖ If S is not a parameterized type or a raw type, then T must implement S,<br />

and the cast is statically known to be correct, or a compile-time error<br />

occurs.<br />

❖ Otherwise, S is either a parameterized type that is an invocation of some<br />

generic type declaration G, or a raw type corresponding to a generic type<br />

declaration G. <strong>The</strong>n there must exist a supertype X of T, such that X is an<br />

invocation of G, or a compile-time error occurs. Furthermore, if S and X<br />

are provably distinct parameterized types then a compile-time error<br />

occurs.<br />

DRAFT<br />

• If S is a type variable, then this algorithm is applied recursively, using the<br />

upper bound of S in place of S.<br />

• If S is an array type SC[], that is, an array of components of type SC :<br />

◆ If T is a class type, then if T is not Object, then a compile-time error occurs<br />

(because Object is the only class type to which arrays can be assigned).<br />

◆ If T is an interface type, then a compile-time error occurs unless T is the<br />

type java.io.Serializable or the type Cloneable, the only interfaces<br />

implemented by arrays.<br />

◆ If T is a type variable, then:<br />

103

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!