Java中ClassCastException的解释 [英] Explanation of ClassCastException in Java

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

问题描述

我阅读了一些关于ClassCastException"的文章,但我无法很好地理解它的含义.什么是 ClassCastException?

I read some articles written on "ClassCastException", but I couldn't get a good idea on what it means. What is a ClassCastException?

推荐答案

直接来自 ClassCastException:

抛出表示代码有试图将对象强制转换为它不是它的子类实例.

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.

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.

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

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