23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

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.

convert, a type <strong>in</strong>to another type. Such conversions may have to be specified by<br />

an explicit cast operator. We have already discussed (Section 1.3.3) how<br />

conversions <strong>and</strong> cast<strong>in</strong>g work for base types. Next, we discuss how they work for<br />

reference variables.<br />

2.5 Cast<strong>in</strong>g <strong>and</strong> Generics<br />

In this section, we discuss cast<strong>in</strong>g among reference variables, as well as a technique,<br />

called generics, which allow us to avoid explicit cast<strong>in</strong>g <strong>in</strong> many cases.<br />

2.5.1 Cast<strong>in</strong>g<br />

We beg<strong>in</strong> our discussion with methods for type conversions for objects.<br />

Widen<strong>in</strong>g Conversions<br />

A widen<strong>in</strong>g conversion occurs when a type T is converted <strong>in</strong>to a "wider" type U.<br />

The follow<strong>in</strong>g are common cases of widen<strong>in</strong>g conversions:<br />

• T <strong>and</strong> U are class types <strong>and</strong> U is a superclass of T<br />

• T <strong>and</strong> U are <strong>in</strong>terface types <strong>and</strong> U is a super<strong>in</strong>terface of T<br />

• T is a class that implements <strong>in</strong>terface U.<br />

Widen<strong>in</strong>g conversions are automatically performed to store the result of an<br />

expression <strong>in</strong>to a variable, without the need for an explicit cast. Thus, we can<br />

directly assign the result of an expression of type T <strong>in</strong>to a variable v of type U<br />

when the conversion from T to U is a widen<strong>in</strong>g conversion. The example code<br />

fragment below shows that an expression of type Integer (a newly constructed<br />

Integer object) can be assigned to a variable of type Number.<br />

Integer i = new Integer(3);<br />

Number n = i;<br />

to Number<br />

// widen<strong>in</strong>g conversion from Integer<br />

The correctness of a widen<strong>in</strong>g conversion can be checked by the compiler <strong>and</strong> its<br />

validity does not require test<strong>in</strong>g by the <strong>Java</strong> run-time environment dur<strong>in</strong>g program<br />

execution.<br />

Narrow<strong>in</strong>g Conversions<br />

A narrow<strong>in</strong>g conversion occurs when a type T is converted <strong>in</strong>to a "narrower"<br />

type S. The follow<strong>in</strong>g are common cases of narrow<strong>in</strong>g conversions:<br />

122

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

Saved successfully!

Ooh no, something went wrong!