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 or Cloneable (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 (because Object 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 type Cloneable (the only interfaces implemented by arrays).

  • If T is a type variable, then:

    • If the upper bound of T is Object or java.io.Serializable or Cloneable, 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的更多相关文章

  1. 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 ...

  2. C#中的值类型(value type)与引用类型(reference type)的区别

    ylbtech- .NET-Basic:C#中的值类型与引用类型的区别 C#中的值类型(value type)与引用类型(reference type)的区别 1.A,相关概念返回顶部     C#中 ...

  3. Welcome-to-Swift-18类型转换(Type Casting)

    类型转换是一种检查类实例的方式,并且哦或者也是让实例作为它的父类或者子类的一种方式. Type casting is a way to check the type of an instance, a ...

  4. 引用类型 (Reference Type Matters)、扩展与派发方式

    引用类型 (Reference Type Matters) 引用的类型决定了派发的方式. 这很显而易见, 但也是决定性的差异. 一个比较常见的疑惑, 发生在一个协议拓展和类型拓展同时实现了同一个函数的 ...

  5. 快速了解C# 8.0中“可空引用类型(Nullable reference type)”语言特性

    Visual C# 8.0中引入了可空引用类型(Nullable reference type),通过编译器提供的强大功能,帮助开发人员尽可能地规避由空引用带来的代码问题.这里我大致介绍一下可空引用类 ...

  6. java中Number Type Casting(数字类型强转)的用法

    4.5 Number Type Casting(数字类型强转)隐式 casting(from small to big) byte a = 111; int b = a;显式 casting(from ...

  7. system verilog中的类型转换(type casting)、位宽转换(size casting)和符号转换(sign casting)

    类型转换 verilog中,任何类型的任何数值都用来给任何类型赋值.verilog使用赋值语句自动将一种类型的数值转换为另一种类型. 例如,当一个wire类型赋值给一个reg类型的变量时,wire类型 ...

  8. [Java in NetBeans] Lesson 03. More Variables / Type Casting

    这个课程的参考视频在youtube. 主要学到的知识点有: It is different from python, that "1" only present string &q ...

  9. C# Value type vs Reference type

    [MY NOTE]   [转载请注明出处] Reference Source: http://www.albahari.com/valuevsreftypes.aspx http://www.c-sh ...

随机推荐

  1. Centos 7 搭建wordpress

    1.安装mysql 详情见:http://www.cnblogs.com/jw35/p/6044170.html 2.关闭firewalld与selinux systemctl stop firewa ...

  2. 还原bak到localdb的问题:The logical database file cannot be found ldf

    主要环境相关因素:win7,ms sql 2012,ms localdb,msms 2012. 步骤: 1,让DBA给一个bak文件到本地来做测试,DBA按自己的工作流程得到bak文件. 2,在msm ...

  3. 重拾C语言基础知识

    从实习到工作两年多的时间了,虽然感觉学到了很多知识,但是事实上却将立足之本的基础知识给忘了个精光.也许跟自己没有出去找工作有关,没有好好的将C语言的基础牢牢掌握. 从现在开始吧!好好的重温基础,做一名 ...

  4. How do I determine if I'm being run under the debugger?

    #include <assert.h>#include <stdbool.h>#include <sys/types.h>#include <unistd.h ...

  5. Android-自定义控件之绘图基础

    画圆形,效果图: 布局中去指定自定义View: <view.custom.androidcustomviewbook.a_draw_base.BaseView android:layout_wi ...

  6. AngularJS Backbone.js Ember.js 对比

    看到一篇关于AngularJS Backbone Ember.js的对比,建议看一看 说说个人的观点(本人学艺不精,只是个人的观点,不保证观点完全正确,请轻拍): backbone.js 短小精悍,非 ...

  7. Solr之functionQuery(函数查询)

    Solr函数查询 让我们可以利用 numeric域的值 或者 与域相关的的某个特定的值的函数,来对文档进行评分. 怎样使用函数查询 这里主要有两种方法可以使用函数查询,这两种方法都是通过solr ht ...

  8. WCF快速上手(二)

    服务端是CS程序,客户端(调用者)是BS程序 一.代码结构: 二.服务接口Contract和实体类Domain INoticeService: using Domain; using System; ...

  9. java学习笔记—HttpServletResponse(21)

    public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, ...

  10. jmeter进行https协议的测试

    一.HTTPS和HTTP的区别     超文本传输协议HTTP协议被用于在Web浏览器和网站服务器之间传递信息.HTTP协议以明文方式发送内容,不提供任何方式的数据加密,如果攻击者截取了Web浏览器和 ...