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.

iresult = i / d; // loss of precision −><br />

this is a compilation error<br />

iresult = (<strong>in</strong>t) i / d;<br />

fractional part is lost<br />

// iresult is 0, s<strong>in</strong>ce the<br />

Note that s<strong>in</strong>ce <strong>Java</strong> will not perform implicit casts where precision is lost, the<br />

explicit cast <strong>in</strong> the last l<strong>in</strong>e above is required.<br />

S<strong>in</strong>ce <strong>Java</strong> 5.0, there is a new k<strong>in</strong>d of implicit cast, for go<strong>in</strong>g between Number<br />

objects, like Integer <strong>and</strong> Float, <strong>and</strong> their related base types, like <strong>in</strong>t <strong>and</strong><br />

float. Any time a Number object is expected as a parameter to a method, the<br />

correspond<strong>in</strong>g base type can be passed. In this case, <strong>Java</strong> will perform an implicit<br />

cast, called autobox<strong>in</strong>g, which will convert the base type to its correspond<strong>in</strong>g<br />

Number object. Likewise, any time a base type is expected <strong>in</strong> an expression<br />

<strong>in</strong>volv<strong>in</strong>g a Number reference, that Number object is changed to the<br />

correspond<strong>in</strong>g base type, <strong>in</strong> an operation called unbox<strong>in</strong>g.<br />

There are few caveats regard<strong>in</strong>g autobox<strong>in</strong>g <strong>and</strong> unbox<strong>in</strong>g, however. One is that if<br />

a Number reference is null, then try<strong>in</strong>g to unbox it will generate an error, called<br />

NullPo<strong>in</strong>terException. Second, the operator, "==", is used both to test the<br />

equality of base type values as well as whether two Number references are<br />

po<strong>in</strong>t<strong>in</strong>g to the same object. So when test<strong>in</strong>g for equality, try to avoid the implicit<br />

casts done by autobox<strong>in</strong>g <strong>and</strong> unbox<strong>in</strong>g. F<strong>in</strong>ally, implicit casts, of any k<strong>in</strong>d, take<br />

time, so we should try to m<strong>in</strong>imize our reliance on them if performance is an<br />

issue.<br />

Incidentally, there is one situation <strong>in</strong> <strong>Java</strong> when only implicit cast<strong>in</strong>g is allowed,<br />

<strong>and</strong> that is <strong>in</strong> str<strong>in</strong>g concatenation. Any time a str<strong>in</strong>g is concatenated with any<br />

object or base type, that object or base type is automatically converted to a str<strong>in</strong>g.<br />

Explicit cast<strong>in</strong>g of an object or base type to a str<strong>in</strong>g is not allowed, however.<br />

Thus, the follow<strong>in</strong>g assignments are <strong>in</strong>correct:<br />

Str<strong>in</strong>g s = (Str<strong>in</strong>g) 4.5; ;<br />

wrong!<br />

// this is<br />

Str<strong>in</strong>g t = "Value = " + (Str<strong>in</strong>g) 13;// this is<br />

wrong!<br />

Str<strong>in</strong>g u = 22;<br />

// this is wrong!<br />

To perform a conversion to a str<strong>in</strong>g, we must <strong>in</strong>stead use the appropriate<br />

toStr<strong>in</strong>g method or perform an implicit cast via the concatenation operation.<br />

Thus, the follow<strong>in</strong>g statements are correct:<br />

50

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

Saved successfully!

Ooh no, something went wrong!