Rtti Study Notes

Source: Internet
Author: User

After reading rtti last night, I suddenly got a question: What is rtti? How does it look like reflection? I went online with this question and read a lot of information, I tried again to draw a conclusion.

Before learning about rtti and reflection, you need to understand the knowledge points and check during compilation and runtime.

Check during compilation: the compiler reads the. Class file during compile to verify file compliance.

Runtime check: when the program is running, read the. Class file to verify file compliance.

One is at compilation and the other is at runtime. How are the two differentiated?

A simple method helps you identify the issue. You call a method of a non-existent Class Using Reflection. If it is executed by a compiler, an error is certainly returned during compilation;
If the program is executed during the execution period, an error is reported during execution.

Class animal {public static void main (string [] ARGs) throws classnotfoundexception {// compiler execution. If the duck. Class file does not exist locally, an error is reported during compilation. Object obj2 = duck. Class; // an error is reported during running. The compiler cannot perform the test during compilation. Object OBJ = Class. forname ("com. Test. DTO. Duck ");}}

Rtti and reflection networks are called.

First, rtti is divided into two parts: the traditional rtti and the reflection.

Second, the two are independent concepts, reflection is not part of rtti, and they are not covered.


So RttiWhat is it and how to understand it?

Rtti literal explanation: runtime type information, runtime type information. Focus: rtti requires the. Class file during the compilation period.

There are three rtti representations:

1. upcasting and downcasting requires forced type conversion in Java.

public class Duck extends Animal{    public void swim(){};    public static void main(String[] args) throws IllegalAccessException, InstantiationException {        Animal animal = new Duck();        ((Duck) animal).swim();    }}class Animal {}
Why does rtti need the. Class file during the compilation period? Check the above Code. If the duck. Class file is not found during the compilation period, an error will be reported during the compilation.


2. Class Object (class object is used, which does not mean reflection. If only Class Object cast is used as the specified class, it is still the traditional rtti object)

Public static void main (string [] ARGs) throws classnotfoundexception {animal = new duck (); Class <duck> clazz = duck. class; duck = clazz. cast (animal); // equivalent to // duck = (duck) animal; duck. swim ();}

Similarly, if there is no duck. Class file, you can skip it.

3. instanceof or isinstance ()

    public static void main(String[] args) throws ClassNotFoundException {        Animal animal = new Duck();        if (animal instanceof  Duck) {            ((Duck) animal).swim();        }    }

Reflection

In contrast to rtti, reflection does not require a. Class file in the compilation phase, that is, reflection does not require a compile-time check. The specific application scenario is that when Java is called remotely, the other party returns you a classname string, representing your class name. Then you can use the reflection mechanism to read your. Class file through classname and call the corresponding method.

Of course, the disadvantage of reflection is that errors cannot be found during the compilation period. In the following classes, once the duck. class cannot be found locally, the classnotfoundexception exception occurs during program running.

class ReflectTest {    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException {        Class c = Class.forName("com.test.dto.Duck");        for (Constructor<?> constructor: c.getConstructors())            System.out.println(constructor);        for (Method method: c.getMethods())            System.out.println(method);        for (Field field: c.getFields())            System.out.println(field);        Method method = c.getMethod("swim");        method.invoke(c.newInstance());    }}

In summary, the difference between rtti and reflection is that reflection can bypass the compile-time check, and rtti does not work.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.