5.2
Assignment Conversion
CONVERSIONS AND PROMOTIONS
char c = \u0123 ;
long l = c;
//
widen
char
to
long
System.out.println("l=0x" + Long.toString(l,16));
f = 1.23f;
double d = f;
//
widen
float
to
double
System.out.println("d=" + d);
}
}
It produces the following output:
f=12.0
i=0x123
d=1.2300000190734863
The following test, however, produces compile time errors:
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.
A value of reference type must not be assigned to a variable of primitive type;
an attempt to do so will result in a compile time error.
A value of the null type (the null reference is the only such value) may be
assigned to any reference type, resulting in a null reference of that type.
Here is a sample program illustrating assignments of references:
public class Point { int x, y; }
public class Point3D extends Point { int z; }
public interface Colorable {
void setColor(int color);
}
public class ColoredPoint extends Point implements Colorable
{
int color;
public void setColor(int color) { this.color = color; }
}
62
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved