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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

5.2 Assignment Conversion CONVERSIONS AND PROMOTIONS<br />

96<br />

System.out.println("l=0x" + Long.toString(l,16));<br />

f = 1.23f;<br />

double d = f; // widen float to double<br />

System.out.println("d=" + d);<br />

}<br />

}<br />

It produces the following output:<br />

f=12.0<br />

l=0x123<br />

d=1.2300000190734863<br />

<strong>The</strong> following test, however, produces compile-time errors:<br />

class Test {<br />

public static void main(String[] args) {<br />

short s = 123;<br />

char c = s; // error: would require cast<br />

s = c; // error: would require cast<br />

}<br />

}<br />

because not all short values are char values, and neither are all char values<br />

short values.<br />

A value of the null type (the null reference is the only such value) may be<br />

assigned to any reference type, resulting in a null reference of that type.<br />

Here is a sample program illustrating assignments of references:<br />

public class Point { int x, y; }<br />

public class Point3D extends Point { int z; }<br />

public interface Colorable {<br />

void setColor(int color);<br />

}<br />

public class ColoredPoint extends Point implements Colorable<br />

{<br />

int color;<br />

public void setColor(int color) { this.color = color; }<br />

}<br />

class Test {<br />

public static void main(String[] args) {<br />

// Assignments to variables of class type:<br />

Point p = new Point();<br />

p = new Point3D(); // ok: because Point3D is a<br />

// subclass of Point<br />

DRAFT

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

Saved successfully!

Ooh no, something went wrong!