当前位置: 技术问答>java相关
谁能给我解释这个问题?
来源: 互联网 发布时间:2015-04-04
本文导语: 在JDK中关于ClassCastException的解释是这样的 Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance. For example, the following code generates a ClassCastException Object x = new...
在JDK中关于ClassCastException的解释是这样的
Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance. For example, the following code generates a ClassCastException
Object x = new Integer(0);
System.out.println((String)x);
My Question:
How cann't generates a ClassCastException?
Thanks!
Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance. For example, the following code generates a ClassCastException
Object x = new Integer(0);
System.out.println((String)x);
My Question:
How cann't generates a ClassCastException?
Thanks!
|
下朔造型错误,String和Integer的超类均为Object,请仔细看一下TIJ中猫和狗的例子
|
如:
java.until.Hashtable a = new Hashtable();
a.put(new Integer(1), new String("20"));
Integer i = (Integer)a.get(new Integer(1));
java.until.Hashtable a = new Hashtable();
a.put(new Integer(1), new String("20"));
Integer i = (Integer)a.get(new Integer(1));