First page Back Continue Last page Graphics
Java Coercion (Casting)
For primitive types, casting can be done only when it makes sense to do so:
- d = (double)true; //NO-incompatible types
- i = (int)d; // OK-but info loss possible
For class types, casting can be done only when it is "down" the class hierarchy. Suppose OtherClass is not a direct or indirect subclass of SuperClass:
- super = (SuperClass)(new OtherClass()); // NO - incompatible types
- sub = (Subclass)super; // OK