Java Reference
In-Depth Information
l=0x123
d=1.2300000190734863
The following program, however, produces compile-time errors:
Click here to view code image
class Test {
public static void main(String[] args) {
short s = 123;
char c = s; // error: would require cast
s = c;
// error: would require cast
}
}
because not all short values are char values, and neither are all char values short values.
Example 5.2-2. Assignment Conversion for Reference Types
Click here to view code image
class Point { int x, y; }
class Point3D extends Point { int z; }
interface Colorable { void setColor(int color); }
class ColoredPoint extends Point implements Colorable {
int color;
public void setColor(int color) { this.color = color; }
}
class Test {
public static void main(String[] args) {
// Assignments to variables of class type:
Point p = new Point();
p = new Point3D();
// OK because Point3D is a subclass of Point
Point3D p3d = p;
// Error: will require a cast because a Point
// might not be a Point3D (even though it is,
// dynamically, in this example.)
// Assignments to variables of type Object:
Object o = p; // OK: any object to Object
int[] a = new int[3];
Object o2 = a; // OK: an array to Object
// Assignments to variables of interface type:
ColoredPoint cp = new ColoredPoint();
Colorable c = cp;
// OK: ColoredPoint implements Colorable
// Assignments to variables of array type:
Search WWH ::




Custom Search