Reference Type Casting
5.5.1. 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|
<:|T|, or |T|<:|S|. 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.
package test5;
interface II {}
class XY<T> {}
class T extends XY<Integer>implements II {}
class S extends XY<Number>implements II {}
public class main1 {
public static void main(String args[]) {
II a = new S();
T t = (T) a; // test5.S cannot be cast to test5.T
}
}
If T is an interface type:
If S is not a
finalclass (§8.1.1), then, 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, and that the erasures of X and Y are the same, a compile-time error occurs.Otherwise, the cast is always legal at compile time (because even if S does not implement T, a subclass of S might).
If S is a
finalclass (§8.1.1), then S must implement T, or a compile-time error occurs.
If T is a type variable, then this algorithm is applied recursively, using the upper bound of T in place of T.
interface I{}
class S implements I {}
public class main2<B extends I,T extends B> {
public void test(){
T d = (T)new S(); // Type safety: Unchecked cast from S to D
}
}
If T is an array type, then S must be the class
Object, or a compile-time error occurs.
If S is an interface type:
If T is an array type, then S must be the type
java.io.SerializableorCloneable(the only interfaces implemented by arrays), or a compile-time error occurs.If T is a type that is not
final(§8.1.1), then 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, and that the erasures of X and Y are the same, a compile-time error occurs.Otherwise, the cast is always legal at compile time (because even if T does not implement S, a subclass of T might).
If T is a type that is
final, then:If S is not a parameterized type or a raw type, then T must implement S, or a compile-time error occurs.
Otherwise, S is either a parameterized type that is an invocation of some generic type declaration G, or a raw type corresponding to a generic type declaration G. Then there must exist a supertype X of T, such that X is an invocation of G, or a compile-time error occurs.
Furthermore, if S and X are provably distinct parameterized types then a compile-time error occurs.
If S is a type variable,
then this algorithm is applied recursively, using the upper bound of S in place of S.
If S is an intersection type A1 & ... & An,
then it is a compile-time error if there exists an Ai (1 ≤ i ≤ n) such that S cannot be cast to Ai by this algorithm. That is, the success of the cast is determined by the most restrictive component of the intersection type.
If S is an array type SC[], that is, an array of components of type SC:
If T is a class type, then if T is not
Object, then a compile-time error occurs (becauseObjectis the only class type to which arrays can be assigned).If T is an interface type, then a compile-time error occurs unless T is the type
java.io.Serializableor the typeCloneable(the only interfaces implemented by arrays).If T is a type variable, then:
If the upper bound of T is
Objectorjava.io.SerializableorCloneable, or a type variable that S could undergo casting conversion to, then the cast is legal (though unchecked).If the upper bound of T is an array type TC
[], then a compile-time error occurs unless the type SC[]can undergo casting conversion to TC[].Otherwise, a compile-time error occurs.
If T is an array type TC
[], that is, an array of components of type TC, then a compile-time error occurs unless one of the following is true:TC and SC are the same primitive type.
TC and SC are reference types and type SC can undergo casting conversion to TC.
Reference Type Casting的更多相关文章
- A const field of a reference type other than string can only be initialized with null Error [duplicate]
I'm trying to create a 2D array to store some values that don't change like this. const int[,] hiveI ...
- C#中的值类型(value type)与引用类型(reference type)的区别
ylbtech- .NET-Basic:C#中的值类型与引用类型的区别 C#中的值类型(value type)与引用类型(reference type)的区别 1.A,相关概念返回顶部 C#中 ...
- Welcome-to-Swift-18类型转换(Type Casting)
类型转换是一种检查类实例的方式,并且哦或者也是让实例作为它的父类或者子类的一种方式. Type casting is a way to check the type of an instance, a ...
- 引用类型 (Reference Type Matters)、扩展与派发方式
引用类型 (Reference Type Matters) 引用的类型决定了派发的方式. 这很显而易见, 但也是决定性的差异. 一个比较常见的疑惑, 发生在一个协议拓展和类型拓展同时实现了同一个函数的 ...
- 快速了解C# 8.0中“可空引用类型(Nullable reference type)”语言特性
Visual C# 8.0中引入了可空引用类型(Nullable reference type),通过编译器提供的强大功能,帮助开发人员尽可能地规避由空引用带来的代码问题.这里我大致介绍一下可空引用类 ...
- java中Number Type Casting(数字类型强转)的用法
4.5 Number Type Casting(数字类型强转)隐式 casting(from small to big) byte a = 111; int b = a;显式 casting(from ...
- system verilog中的类型转换(type casting)、位宽转换(size casting)和符号转换(sign casting)
类型转换 verilog中,任何类型的任何数值都用来给任何类型赋值.verilog使用赋值语句自动将一种类型的数值转换为另一种类型. 例如,当一个wire类型赋值给一个reg类型的变量时,wire类型 ...
- [Java in NetBeans] Lesson 03. More Variables / Type Casting
这个课程的参考视频在youtube. 主要学到的知识点有: It is different from python, that "1" only present string &q ...
- C# Value type vs Reference type
[MY NOTE] [转载请注明出处] Reference Source: http://www.albahari.com/valuevsreftypes.aspx http://www.c-sh ...
随机推荐
- Android如何判断当前手机是否正在播放音乐,并获取到正在播放的音乐的信息
我想实现如下的场景,判断当前Android手机上是否正在播放音乐,如果是,通过某个特定的手势, 或者点击某个按键,将当前我正在听的音乐共享出去. 第一步,就是判断当前是否有音乐正在播放. 最开始我想得 ...
- ZOJ 3702 Gibonacci number 2017-04-06 23:28 28人阅读 评论(0) 收藏
Gibonacci number Time Limit: 2 Seconds Memory Limit: 65536 KB In mathematical terms, the normal ...
- [51单片机] nRF24L01 无线模块 串口法命令 通过无线控制另一个的灯
>_<!概述: 这是在上一个的基础上通过按键发送4种不同命令来控制接收端的LED灯亮的改进版(上一个:http://www.cnblogs.com/zjutlitao/p/3840013. ...
- 「BZOJ1426」收集邮票
题目链接 戳我 \(Solution\) 我们首先转换一下问题: 假设我们进行了k轮得到了所有种类的邮票 则所花费用为: \[(1+2+5+...+k)=\frac{(1+k)*k}{2}=\frac ...
- C博客的第1次作业--分支,顺序结构
1.本章学习总结 1.1 思维导图 1.2本章学习体会,代码量学习体会 1.2.1学习体会 初步了解什么是C语言,明白了这门语言的基本运行功能.了解了关于c语言结构上,语法上的基本知识.下一步要进一步 ...
- Vue的组件和路由
组件 组件 (Component) 是 Vue.js 最强大的功能之一.组件可以扩展 HTML 元素,封装可重用的代码.在较高层面上,组件是自定义元素,Vue.js 的编译器为它添加特殊功能. 组件的 ...
- sgi stl内存池实现------源码加翻译
class __default_alloc_template { enum { unit = 8 };//分配单位 后面直接用8代替 enum { max_bytes = 128 };//最大分配字节 ...
- HTTP协议基础(未完待续)
一.超文本传输协议 超文本传输协议(Hypertext Transfer Protocol,HTTP)是一种用于分布式.协作式和超媒体信息系统的应用层协议.HTTP是万维网的数据通信的基础. 设计HT ...
- jQuery中的each, data, 插件
一. each() $(' ').each(function (){...}) jQuery.each(collection, callback(indexInArray, valueOfEleme ...
- 自注意力机制(Self-attention Mechanism)——自然语言处理(NLP)
近年来,注意力(Attention)机制被广泛应用到基于深度学习的自然语言处理(NLP)各个任务中.随着注意力机制的深入研究,各式各样的attention被研究者们提出.在2017年6月google机 ...