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 Narrowing Reference Conversions 5.1.6<br />

• byte to char<br />

First, the byte is converted to an int via widening primitive conversion, and then<br />

the resulting int is converted to a char by narrowing primitive conversion.<br />

5.1.5 Widening Reference Conversions<br />

A widening reference conversion exists from any type S to any type T, provided S<br />

is a subtype (§4.10) of T.<br />

Widening reference conversions never require a special action at run time and<br />

therefore never throw an exception at run time. <strong>The</strong>y consist simply in regarding a<br />

reference as having some other type in a manner that can be proved correct at<br />

compile time.<br />

See §8 for the detailed specifications for classes, §9 for interfaces, and §10 for<br />

arrays.<br />

5.1.6 Narrowing Reference Conversions<br />

<strong>The</strong> following conversions are called the narrowing reference conversions :<br />

• From any reference type S to any reference type T, provided that S is a<br />

proper supertype (§4.10) of T. (An important special case is that there is a narrowing<br />

conversion from the class type Object to any other reference type.)<br />

• From any class type C to any non-parameterized interface type K, provided<br />

that C is not final and does not implement K.<br />

• From any interface type J to any non-parameterized class type C that is not<br />

final.<br />

• From the interface types Cloneable and java.io.Serializable to any<br />

array type T[].<br />

• From any interface type J to any non-parameterized interface type K, provided<br />

that J is not a subinterface of K.<br />

• From any array type SC[] to any array type TC[], provided that SC and TC<br />

are reference types and there is a narrowing conversion from SC to TC.<br />

DRAFT<br />

Such conversions require a test at run time to find out whether the actual reference<br />

value is a legitimate value of the new type. If not, then a ClassCastException is<br />

thrown.<br />

85

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

Saved successfully!

Ooh no, something went wrong!