Java Type information

Source: Internet
Author: User
Tags reflection

rtti--Run-time type information (run-time type information)that can be used by the run-time type information program to examine the actual derived type of the object that the pointer or reference refers to by using a pointer or reference to the base class.

RTTI provides the following two very useful operators:

(1) The typeid operator, which returns the actual type that the pointer and reference refer to.

(2) The dynamiccast operator, which converts a pointer or reference of a base class type safely to a pointer or reference of a derived type.

code example:

1  Public classTest1 {2     Public Static voidMain (string[] args) {3      4       //when the Square,triangle object is placed in the list, it is transformed upward to shape .5list<shape> shapelist = Arrays.aslist (NewShape (),NewSquare (),NewTriangle ());6 7       //The polymorphic mechanism ensures that the Shape object executes what code is determined by the specific object to which the reference is directed9        for(Shape shape:shapelist) {Ten Shape.draw (); One       } A    } -}

Operation Result:

I am the draw () method of the base class shape!

I am the draw () method of the subclass square!

I am the draw () method of sub-class triangle!

Summary: Using RTTI, you can query the exact type of the object that a shape reference points to, and then select or reject the case. Note: Rtti allows the discovery of class information through anonymous base class references, but it is important to be aware of the need to use polymorphic mechanisms to prioritize polymorphism, otherwise, in the code development and maintenance process, loss of a lot of the value of polymorphic mechanisms, when necessary to choose Rtti.

RTTI in the Java working principle in:

In Java, there is a special object called a class object that contains information about the classes, which is performed by using the class object, and whenever a new class is written and then compiled, the RTTI subsystem of the Java Virtual machine runs the program. When a program creates the first reference to a static member of a class, the class is loaded, stating that the constructor of the class is also a static method of the class, even if there is no static keyword decoration before the constructor, so you can also consider a reference to a static member of a class when you create a class object using the new operator.

Methods for obtaining a class object reference:

1. Specific objects. GetClass (): This method can be used when holding objects of this type

2. Class.forName (): You do not need to hold objects of this type in order to obtain a Class reference. Note, however, that you want to put in the Try-catch clause, because if this class is not found, ClassNotFoundException will be thrown. Gets the reference to the class at the same time that it is initialized.

3. Class literal constants. It will be checked at compile time without try-catch. Using class literal constants to obtain a reference to a class does not cause initialization, and initialization implements "lazy", which is deferred until the static method (the constructor is implicitly static) or the first reference to a very few static domain.

 Packageclassinformation;classCandy {Static{System.out.println ("Loading Candy"); }}classGum {Static{System.out.println ("Loading Gum"); } }classCookie {Static{System.out.println ("Loading Cookie")); }} Public classsweetshop { Public Static voidMain (string[] args) {//The first way:Candy c=NewCandy (); System.out.println ("Created object candy information:" +C.getclass ()); //The second way:        Try{class.forname ("Gum"); //so strange, why is there no loading gum output? }Catch(ClassNotFoundException e) {System.out.println ("Couldn ' t find Gum"); }        //The Third Way:Class cc = Cookie.class;
System.out.println (Cookie.class); }}

Program Run Result:

Loading Candy

Created object Candy information: Class Classinformation.candy

Couldn ' t find Gum

Class Classinformation.cookie

Type conversions:

1. The downward transformation requires an explicit transformation operation;

2. The upward steering, does not need the explicit transformation operation;

3. Keyword instanceof, you can use it in the way of questioning, to determine whether an object is an instance of that feature type;

Reflection: Class information at run time

Java provides a build-based programming framework through JavaBeans that provides a way to create and run objects on a remote platform across the network through remote Method invocation (Invocation,rmi). The class class and the Java.lang.reflection class library support the concept of reflection, dealing with an object of unknown type by reflection, first the JVM checks which class the object belongs to, and obtains the available methods of that type through the class method extractor.

Java Type information

Related Article

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.