CONVERSIONS AND PROMOTIONS
Assignment Conversion
5.2
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:
byte[] b = new byte[4];
a = b;
//
error: these are not arrays
//
of the same primitive type
Point3D[] p3da = new Point3D[3];
Point[] pa = p3da;
//
ok: since we can assign a
// Point3D
to a
Point
p3da = pa;
//
error: (cast needed) since a
Point
//
can t be assigned to a
Point3D
}
}
Assignment of a value of compile time reference type
S
(source) to a variable
of compile time reference type
T
(target) is checked as follows:
If
S
is a class type:
N
If
T
is a class type, then
S
must either be the same class as
T
, or
S
must be a
subclass of
T
, or a compile time error occurs.
N
If
T
is an interface type, then
S
must implement interface
T
, or a compile
time error occurs.
N
If
T
is an array type, then a compile time error occurs.
63
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