我认为,如果你的程序设计思路清晰,编制正确,一般是不会产生ClassCastException的。
所以,ClassCastException是作为RuntimeException的子类而设计的。

解决方案 »

  1.   

    Object x=new Integer(0);
    System.out.println((String)x); //已经执行了casting(强制转换),当然不会产生ClassCastException了
      

  2.   

    你们看看那段英文,从这句话 For example, the following code generates a ClassCastException 可以看出,它说的意思是那段代码会Cast ClassCastException,我想问,怎么样改那段代码,让它不Cast ClassCastException.
      

  3.   

    ClassCastException 也就是将一个类,强制转化为它的子类是会抛出的异常。在例子中System.out.println((String)x);会抛出这样的异常的,因为它将一个OBJECT强制转成了STRING了!
      

  4.   

    下朔造型错误,String和Integer的超类均为Object,请仔细看一下TIJ中猫和狗的例子
      

  5.   

    以Object为返回值得函数,如:hashtable,枚举中获得值,降值转换成和原先赋值不同的类型的类时,就会有这样的异常了。
      

  6.   

    如:
    java.until.Hashtable a = new Hashtable();
    a.put(new Integer(1), new String("20"));
    Integer i = (Integer)a.get(new Integer(1));