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 ...
随机推荐
- Charles网络工具
Charles 是在 Mac 下常用的网络封包截取工具,在做移动开发时,我们为了调试与服务器端的网络通讯协议,常常需要截取网络封包来分析. Charles 通过将自己设置成系统的网络访问代理服务器,使 ...
- openfire搭建spackweb在线即时聊天
1.首先去openFire官网下载openFire以及spackweb,以下地址可以2样东西一次打包下载.http://download.csdn.net/detail/a315157973/8048 ...
- Android源码设计模式分析开源项目
简述 该项目通过分析Android系统中的设计模式来提升大家对设计模式的理解,从源码的角度来剖析既增加了对Android系统本身的了解,也从优秀 的设计中领悟模式的实际运用以及它适用的场景,避免在实际 ...
- Tomcat启动报错Invalid character found in method name. HTTP method names must be tokens
1.tomcat服务器需配置三个端口才能启动,安装时默认启用了这三个端口,当要运行多个tomcat服务时需要修改这三个端口,不能相同. 端口一: 修改http访问端口(默认为8080端口),配置文件为 ...
- C#中哈希表(HashTable)的用法详解
描述: 哈希表存放 key.values ,key值可以用于快速调取用,values 对应object类型,也就是说所有类型. 实例: 1.HashTable存放学生的成绩 Hashtable ht1 ...
- PostgreSQL查询数据(基本查询)
原料:数据表 create table "SysUser"( "UserId" serial, --用户Id,自增 "UserName" ) ...
- 网络正常只有自己访问网站异常一度让你怀疑,是不是被黑了!域名解析异常是如何发生的,如何解决处理及C#编程实现一键修改Hosts文件
首先大家要知道在浏览器上浏览虚拟主机,必须使用Hosts文件或域名系统(DNS)实现主机名到IP地址的解析.在局域网中用Hosts文件或DNS都可以,在Internet上只能用DNS了. 1.当用户输 ...
- 会HTML/CSS就可以轻松创建网站
网站其本质就是HTML + CSS 外加一些JavaScript构成的.所以基本上只要你会一些前端,就可以开始花样搭网站了. 如果只用HTML/CSS那做出来的网站只能叫静态网站,性能好但维护不方便, ...
- linux 动态库加载路径修改
1.在 /etc/ld.so.conf 文件中添加搜索路径,重启或者 ldconfig 生效: 2.在 /etc/ld.so.conf.d 目录下添加 *.conf 文件,其中可以添加搜索路径,重启获 ...
- java学习笔记—HttpServletResponse(21)
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, ...