强制类型转换
强制类型转换时,
如:A a=(B)b; //b 为B类的instance
编译器只检查
1 A是B的父类
2不检查 (B)b 是否合理。这个检查任务在runtime是执行
package test2; public class Demo { public static void main(String[] args) { Parent p=new Parent(); Child c=new Child(); Child c1; Parent p1; p1=(Child)c; p1=(Child)p; //runtime exception :ClassCastException c1=(Parent)c; //编译时报错 Type mismatch: cannot convert from Parent to Child c1=(Parent)p; //编译时报错 Type mismatch: cannot convert from Parent to Child p1=(Parent)c; p1=(Child)p; //runtime exception :ClassCastException
} } class Parent{} class Child extends Parent{}