• Runtime type information(RTTI) allows you to discover and use type information while a program is running.
  • All classes are loaded into the JVM dynamically, upon the first use of a class. This happens when the program makes the first reference to a static member of that class. It turns out that the constructor is also a static method of a class, even though the static keyword is not used for  a constructor. Therefore, creating a new object of that class using the new operator also counts as a reference to a static member of the class.
  • The class loader first checks to see if the Class Object for that type is loaded. If not, the default class loader finds the .class file with that name(an add-on class loader might, for example, look for the bytecodes in a database instead). As the bytes for the class are loaded, they are verified to ensure that they have not been corrupted and that they do not comprise bad Java code(this is one of the lines of defense for security in Java). Once the Class object for that type is in memory, it is used to create all object of that type.
  • Java provides a second way(except Class.forName()) to produce the reference to the Class object: the class literal. This would look like: java.util.ArrayList.class; which is not only simpler, but also safer since it's checked at compile time(and thus does not need to be placed in a try block). Because it eliminates the forName() method call, it's also more efficient. Class literals work with regular classes as well as interfaces, arrays, and primitive types. In addition, there's a standard field called TYPE that exists for each of the primitive wrapper classes. The TYPE field produces a reference to the Class object for the associated primitive type, such as: boolean.class is equivalent to Boolean.TYPE.

  • It's interesting to note that creating a reference to a Class obejct using ".class" doesn't automatically initialize the Class object. There are actually three steps in preparing a class for use:Effectively, initialization is "as lazy as possible." Just using the .class syntax to get a reference to the class doesn't cause initialization. However, Class.forName() initializes the class immediately in order to produce the Class reference. If a static final value is a "compile-time constant", that value can be read without causing the corresponding class to be initialized. Making a field static and final, however, does not guarantee this behavior(forces class initialization if it don't be a compile-time constant). If a static field is not final, accessing it always requires linking(to allocate storage for the field) and initialization(to initialize that storage) before it can be read.
    1. Loading, which is performed by the class loader. This finds the bytecodes(usually, but not necessarily, on your disk in your classpath) and create a Class object from those bytecodes.
    2. Linking. The linke phase verifies the bytecodes in the class, allocates storage for static fields, and if necessary, resovles all references to other classes made by this class.
    3. Initialization. If there's a superclass, initialize that. Execute static initializers and static initialization blocks.
    • 注: Initialization is delayed until the first reference to a static method(the constructor is implicitly static) or to a non-constant static field.
  • Effectively, initialization is "as lazy as possible". Just using the .class syntax to get a reference to a class doesn't cause initialization. However, Class.forName() initializes the class immediately in order to produce the Class reference. If a static final value is a "compile-time constant", that value can be read without causing the corresponding class to be initialized. Making a field static and final, however, does not guarantee this behavior(force class initialization if it don't be a compile-time constant). If a static field is not final, accessing it always requires linking(to allocate storage for the field) and initialization(to initialize that storage) before it can be read.

  • The reason for adding the generic syntax to Class references is only to provide compile-time type checking, so that if you do something wrong you find out about it a little sooner.
  • It's important to use instanceof before a downcast when you don't have other information that tells you the type of the object; otherwise, you'll end up with a ClassCastException.
  • It's important to realize that there's nothing magic about reflection. When you're using reflection to interact with an object of an unknown type, the JVM will simply look at the object and see that it belongs to a particular class(just like an ordinary RTTI). Before anything can be done with it, the Class object must be loaded. Thus, the .class file for that particular type must still be available to the JVM, either on the local machine or across the network. So the true difference between RTTI and reflections is that with RTTI, the compiler opens and examines the .class file at compile time. Put another way, you can call all the methods of an object in the "normal" way. With reflection, the .class file is unavailable at compile time; it is opened and examined by the runtime environment.

静态代理VS动态代理:

对于静态代理,一个委托类对应一个代理类,因此过度使用静态代理会导致大量的代理类产生。于是出现了动态代理,对于相同逻辑的代理,我们只需要动手实现一遍,就可以以相同的处理方式代理不同的委托类。

TIJ——Chapter Fourteen:Type Information的更多相关文章

  1. (TODO:)下载图片,报错:warning: could not load any Objective-C class information from the dyld shared cache. This will significantly reduce the quality of type information available.

    想使用NSInvocationOperation下载图片,然而并没有下载下来, NSData为nil, 还有报错:(打断点就报错) warning: could not load any Object ...

  2. TIJ——Chapter Eight:Polymorphism

    The twist |_Method-call binding Connecting a method call to a method body is called binding. When bi ...

  3. CCJ PRML Study Note - Chapter 1.6 : Information Theory

    Chapter 1.6 : Information Theory     Chapter 1.6 : Information Theory Christopher M. Bishop, PRML, C ...

  4. RTTI(Runtime Type Information )

    RTTI 是“Runtime Type Information”的缩写,意思是:运行时类型信息.它提供了运行时确定对象类型的方法.本文将简略介绍 RTTI 的一些背景知识.描述 RTTI 的概念,并通 ...

  5. Dynamic type checking and runtime type information

    动态类型的关键是将动态对象与实际类型信息绑定. See also: Dynamic programming language and Interpreted language Dynamic type ...

  6. C++ - RTTI(RunTime Type Information)执行时类型信息 具体解释

    RTTI(RunTime Type Information)执行时类型信息 具体解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details ...

  7. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十四)之Type Information

    Runtime type information (RTTI) allow you to discover and use type information while a program is ru ...

  8. RTTI (Run-time type information) in C++

    In C++, RTTI (Run-time type information) is available only for the classes which have at least one v ...

  9. TIJ——Chapter Two:Everything Is an Object

    If we spoke a different language, we would perceive a somewhat different world. Ludwig Wittgenstein( ...

随机推荐

  1. HBase 三维模型解析

    总结下一直想写hbase的实践经验,在用hbase的过程中,我们都知道,rowkey设计的好坏,是我们能最大发挥hbase的架构优势,也是我们是否正确理解hbase的一个关键点.闲话少说,进入正题. ...

  2. 2019-2-19-win10-uwp-客户端如何发送类到-asp-dotnet-core-作为参数

    title author date CreateTime categories win10 uwp 客户端如何发送类到 asp dotnet core 作为参数 lindexi 2019-2-19 9 ...

  3. SSM10-Redis持久化和集群的搭建

    1.1. Redis集群的搭建 Redis集群中至少应该有三个节点.要保证集群的高可用,需要每个节点有一个备份机. Redis集群至少需要6台服务器. 搭建伪分布式.可以使用一台虚拟机运行6个redi ...

  4. 华为 Mate8 Emui 5.0 安卓 7.0 root 记录

    步骤: 0.备份手机全部资料 1.华为官网申请解锁码 (unlock password) http://emui.huawei.com/plugin/hwdownload/download 2.关闭手 ...

  5. .net4.6版本前设置window子窗口位置主窗口闪烁

    在安装了.net4.6的版本是不会出现该问题的,但是在4.6以下的版本会出现,当设置之窗体的left和top属性时,会让主窗体闪烁一下. 之前是在load事件下写的: child_window.loa ...

  6. Vue-Cli 3.0 + vue.config.js

    虽然一直没有亲手搭建过vue项目,但是2.0的时候就开始自学(但并没有实践项目).然后公司最近有个人用3.0做了个项目,公司让我参与进去,我就顺便学习了一把3.0.(美滋滋) 因为电脑的环境还是之前自 ...

  7. I Love Palindrome String

    I Love Palindrome String 时间限制: 2 Sec  内存限制: 128 MB 题目描述 You are given a string S=s1s2..s|S| containi ...

  8. 洛谷P1147 连续自然数和 [2017年6月计划 数论01]

    P1147 连续自然数和 题目描述 对一个给定的自然数M,求出所有的连续的自然数段,这些连续的自然数段中的全部数之和为M. 例子:1998+1999+2000+2001+2002 = 10000,所以 ...

  9. P1561 [USACO12JAN]爬山Mountain Climbing

    P1561 [USACO12JAN]爬山Mountain Climbing 题目描述 Farmer John has discovered that his cows produce higher q ...

  10. 【Ruby】与ruby相关的内容

    本博文是为了记录Ruby相关的可学习资源,以便日常使用 在线学习Rails的网站版本 Ruby on Rails教程(Rails 5) Ruby的中文社区 Ruby China