java强制类型转换_java强制类型转换原理?

原理请参考Java Language Specification:Reference Type Casting 中有关引用类型转换的表述,其中相关部分为:Given a compile-time reference type S (source) and a compile-time reference type T (target), a casting conversion exists from S to T if no compile-time errors occur due to the following rules.

If S is a class type:

•If T is a class type, then either |S| <: or otherwise a compile-time error occurs.>

Furthermore, if there exists a supertype X of T, and a supertype Y of S, such that both X and Y are provably distinct parameterized types (§4.5), and that the erasures of X and Y are the same, a compile-time error occurs.

满足|S| <: or casts and unchecked at run time r is an ordinary class array>

•If T is a class type, then R must be either the same class (§4.3.4) as T or a subclass of T, or a run-time exception is thrown.

因为Container和JComponent之间存在着父子关系,所以根据Java语言规范,保证了他们之间的转换在编译期不会报错,但是具体getContentPane方法的返回值的runtime类型是否满足上述条件,则需要在运行时进行类型检查。具体在VM的实现上,是根据checkcast这个字节码指令来完成的,相关解释规则可以参见JVM规范:Chapter 6. The Java Virtual Machine Instruction Set。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值