“ClassCastException”的解释在Java中 [英] Explanation of "ClassCastException" in Java

查看:112
本文介绍了“ClassCastException”的解释在Java中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了一些关于ClassCastException的文章,但我无法对此有所了解。是否有一篇好文章或什么是简短的解释?

I read some articles written on "ClassCastException", but I couldn't get a good idea on that. Is there a good article or what would be a brief explanation?

推荐答案

直接来自 ClassCastException


抛出以表明代码有
试图将对象强制转换为
子类它不是
实例。

Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance.

因此,例如,当一个人试图投出一个整数字符串字符串不是的子类整数,因此将抛出 ClassCastException

So, for example, when one tries to cast an Integer to a String, String is not an subclass of Integer, so a ClassCastException will be thrown.

Object i = Integer.valueOf(42);
String s = (String)i;            // ClassCastException thrown here.

这篇关于“ClassCastException”的解释在Java中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆