5.5
Casting Conversion
CONVERSIONS AND PROMOTIONS
O
If
T
 is an array type
TC[]
, that is, an array of components of type
TC
,
 then
a run time exception is thrown unless one of the following is true:
TC
 and
RC
 are the same primitive type.
TC
 and
RC
 are reference types and type
RC
 can be cast to
TC
 by a recur 
sive application of these run time rules for casting.
If a run time exception is thrown, it is a
ClassCastException
 ( 11.5.1.1,
 20.22).
Here are some examples of casting conversions of reference types, similar to
the example in  5.2:
public class Point { int x, y; }
public interface Colorable { void setColor(int color); }
public class ColoredPoint extends Point implements Colorable
{
int color;
public void setColor(int color) { this.color = color; }
}
final class EndPoint extends Point { }
class Test {
public static void main(String[] args) {
Point p = new Point();
ColoredPoint cp = new ColoredPoint();
Colorable c;
//
The following may cause errors at run time because
//
we cannot be sure they will succeed; this possibility
//
is suggested by the casts:
cp = (ColoredPoint)p; // p
 might not reference an
//
object which is a
ColoredPoint
//
or a subclass of
ColoredPoint
c = (Colorable)p;
// p
 might not be
Colorable
//
The following are incorrect at compile time because
//
they can never succeed as explained in the text:
Long l = (Long)p;
//
compile time error #1
EndPoint e = new EndPoint();
c = (Colorable)e;
//
compile time error #
2
}
}
Here the first compile time error occurs because the class types
Long
 and
Point
are unrelated (that is, they are not the same, and neither is a subclass of the other),
so a cast between them will always fail.
70






footer




 

 

 

 

 Home | About Us | Network | Services | Support | FAQ | Control Panel | Order Online | Sitemap | Contact

java hosting

 

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