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
final
class (§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
final
class (§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.Serializable
orCloneable
(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 (becauseObject
is 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.Serializable
or the typeCloneable
(the only interfaces implemented by arrays).If T is a type variable, then:
If the upper bound of T is
Object
orjava.io.Serializable
orCloneable
, 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 ...
随机推荐
- GPU的历史:从固定管线到可编程管线再到通用计算平台
开始的时候GPU不能编程,也叫固定管线的,就是把数据按照固定的通路走完. 和CPU同样作为计算处理器,顺理成章就出来了可编程的GPU,但是那时候想在GPU上编程可不是容易的事,你只能使用GPU汇编来写 ...
- [leetcode] 5. Minimum Depth of Binary Tree
二叉树基本功练习题,题目如下: Given a binary tree, find its minimum depth. The minimum depth is the number of node ...
- 个人项目-数组求和(语言:C++)
prog1详细要求: [第一版本程序Prog1要求:] + 给定一个数组,实现数组元素求和:,具体要求:实现对一维数组(a[100])的所有元素相加运算. + 数据准备:a)数组长度:100:b)数组 ...
- Python2 to python3
概述 几乎所有的Python 2程序都需要一些修改才能正常地运行在Python 3的环境下.为了简化这个转换过程,Python 3自带了一个叫做2to3的实用脚本(Utility Script),这个 ...
- 指定的 LINQ 表达式包含对与不同上下文关联的查询的引用
解决方法是分两次查询. 报错的原因是在涉及到内存中的对象与EF里的对象混合查询时,内存中的对象要是基元类型. 第一次查询实际上会因为EF的延时加载,不会立即将数据查询到内存中. 解决方法是对第一次查询 ...
- 浅谈 温故知新——HTML5!
古人有云:温故而知新.活到老,学到老,作为一枚前端的程序猿,不停的学习能够让我们对我们的技术有一个更加丰富的认识.这几天,项目已经完成,但我发现自己的知识体系存在一些短板,特别是在H5方面,所以我又回 ...
- 关于EF的一点小记录
今日新闻:朝鲜要改革开放了!!!比你牛逼的人都在努力,你还有理由懒惰吗? 宇宙强大IDE配套的EF问题记录 今天做数据添加时,Id我设置为int类型了,结果在做Add操作时报的错让我摸不着头脑,后来问 ...
- C# 使用dynamic类型来访问JObject对象
dynamic是C#里面的动态类型,可在未知类型的情况访问对应的属性,非常灵活和方便. 使用Json.Net可以把一个Json字符串转换成一个JObject对象,如果有已知强类型,如果有已知对应的强类 ...
- autofac JSON文件配置
autofac是比较简单易用的IOC容器.下面我们展示如何通过json配置文件,来进行控制反转. 需要用到以下程序集.可以通过nugget分别安装 Microsoft.Extensions.Confi ...
- Android intent 传值不更新的原因和解决办法
当 Activity 的启动模式是 singleTask 或者 singleInstance 的时候.如果使用了 intent 传值,则可能出现 intent 的值无法更新的问题.也就是说每次 int ...