think in java笔记:RTTI-Runtime type information

RTTI-Runtime type information

To understand how RTTI works in Java, you must first know how type information is
represented at run time. This is accomplished through a special kind of object called the
Class object, which contains information about the class. In fact, the Class object is used to
create all of the “regular” objects of your class. Java performs its RTTI using the Class
object, even if you’re doing something like a cast. The class Class also has a number of other
ways you can use RTTI.

译:
为了懂得RTTI是怎么工作的,你必须type信息是如何在运行时候表示的。这个是通过一个特殊的对象Class实现的,这个对象保存着这个class的信息。实际上,Class对象是用来创建所有正常类型的对象。Java通过Class对象来执行RTTI,即使你看起来是在做cast操作。Class这个class还有一些可以使用RTTI的方式。
(class VS Class : class是静态的引用,Class是用在实例化之后getClass之后得到的,两个都可以获取对象的信息.)

It’s important to realize that there’s nothin g 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 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 reflection 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.

译:
我们需要知道反射是没有很神奇的东西。当你使用反射来于一个不知道类型的object操作时,JVM将会寻找它所对应的class。在任何东西可以做之前,Class对象必须已经加载。即.class文件必须存在,不论是在本地或者远程机器上。所以RTTI和反射不同的地方就是:RTTI是只能反射出来编译时的所有类型,但是反射可以知道实际运行过程中所有的类型。

你可能感兴趣的:(Think,in,Java笔记)