我读了一些关于“ClassCastException”的文章,但我不太清楚它的含义。什么是 ClassCastException?
最佳答案
直接来自 ClassCastException
的 API 规范:
Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance.
因此,例如,当尝试将 Integer
转换为 String
时,String
不是 Integer 的子类
,因此将抛出ClassCastException
。
Object i = Integer.valueOf(42);
String s = (String)i; // ClassCastException thrown here.
关于java - Java中ClassCastException的解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62239933/