jianglilili / jianglilili/guodegang.github.io

ClassCastException

Open
#24 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
No language data
Stars
0
Forks
0
PR merge metrics
No merged PRs in 30d

Description

- 当试图将对象强制转换为不是实例的子类时,抛出该异常

```
public class myTest
{
public static void main(String[] args)
{
Object o=new Integer(0);
System.out.println((String) o);
}
}
```

- 说明:上面代码首先创建一个Object对象o,该对象是通过Object的子类Integer创建的,**也就是说Object对象o是子类Integer的实例**,因此当使用String类将对象o强制转换为字符串时出错了,因为对象o是Object的子类Integer创建的,而不是String创建的,所以产生了错误,如果将对象o强制转换为Integer类型就不会出错,因为对象o是Integer类创建的。

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.