Java type information, java type
I. Introduction
I recently read Java programming ideas and learn about java types. Now I will summarize them. Java allows us to identify the object and class information at runtime. There are two main methods: one is the traditional "RTTI", which assumes that we already know all the types during compilation, and the other is the "reflection" mechanism, it allows us to discover and use class information at runtime.
Ii. Class Object
To understand how RTTI works, you must first know how the type information is expressed at runtime. This task is completed by a special object called a Class object, which contains information related to the Class. In fact, the Class object is used to create all the "regular" objects of the Class. Java uses Class objects to execute RTTI, even if you are performing operations like transformation. The Class also has a large number of other methods that use RTTI.
A Class is a part of a program. Each Class has a Class object. In other words, every time a new Class is compiled and compiled, a class object is generated (more appropriately, it is saved in a file with the same name. Class ). To generate the object of this class, the Java Virtual Machine (JVM) running this program uses a subsystem called the class loader.
The Class Loader subsystem can actually contain a Class Loader chain, but there is only one native class loader. It is part of JVM implementation. Native class loaders load so-called trusted classes, including Java API classes, which are usually loaded from local disks. In this chain, there is usually no additional Class Loader, but if you have special requirements (for example, loading classes in a special way to support Web server applications, or download the class from the Network), you can mount this additional Class Loader in one way.
All classes are dynamically loaded to the JVM when they are used for the first time. When the program creates the first static member reference to the class, the class is loaded. This proves that the constructor is also a static method of the class, even if no static keyword is used before the constructor. Therefore, the new class object created using the new operator will also be referenced as a static member of the class.
Iii. Generate Class objects as follows
1. Class. forName ("Class Name string ")
2. class Name. class
3. Instance Object. getClass ()
4. The preparation for using the class actually involves three steps:
1. Load. This is executed by the class loader. This step searches for bytecode (usually in the path indicated by classpath, but not required) and creates a Class object from these bytecode.
2. Link. In the link phase, the bytecode in the verification class is used to allocate storage space for the static domain, and if necessary, all references to other classes are created for this class.
3. initialization. If the class has a superclass, initialize the superclass and execute the static initialization tool and static initialization block. Initialization is delayed until the static method (which is implicitly static by the constructor) or when a very few static fields are referenced for the first time:
The following is an example of initialization.
1 import java.util.Random; 2 3 class Initable{ 4 static final int staticFinal = 47 ; 5 static final int staticFinal2 = 6 ClassInitialization.random.nextInt(1000); 7 static { 8 System.out.println("Initialzing Initable"); 9 }10 11 }12 13 class Initable2{14 static final int staticNonFinal = 147 ;15 static{16 System.out.println(" Initializing Initable2 ");17 }18 }19 20 class Initable3{21 static int staticNoFinal = 74 ;22 static {23 System.out.println("Initializing Initable3");24 }25 }26 27 public class ClassInitialization {28 public static Random random = new Random(47);29 30 public static void main(String[] args) throws Exception{31 Class initable = Initable.class ;32 System.out.println("After creating Initable ref");33 34 System.out.println(Initable.staticFinal);35 36 System.out.println(Initable.staticFinal2);37 38 System.out.println(Initable2.staticNonFinal);39 40 Class initable3 = Class.forName("Initable3") ;41 42 System.out.println("After creating Initable3 ref");43 44 System.out.println(Initable3.staticNoFinal);45 46 } 47 }
The running result is as follows:
1 After creating Initable ref2 473 Initialzing Initable4 2585 1476 Initializing Initable37 After creating Initable3 ref8 74
This example shows:
1. initialize as "inert" as possible ". During the creation of the initable reference, we can see that only the. class syntax is used to obtain the reference to the class. Initialization will not occur. However, to generate Class references, Class. forName () is initialized immediately, as shown in initable3 reference creation.
2. If a static final value is a "compile-time constant", like Initable. staticFinal, this value can be read without initialization of the Initable class. However, if you only set a domain to static and final, it is not enough to ensure this behavior, for example, for Initable. staticfinal's access will force class initialization because it is not a compilation constant.
3. if a static domain is not final, it is always required to connect to it before it is read (allocate storage space for this domain) and initialize (initialize the bucket), as in the Initable. as shown in staticNonFinal access.
V. Class Object method Overview:
1. Class Object Overview
(1) Hold RTTI Information
(2) Each Class has a Class object. Every time a new Class is compiled, a Class object is generated.
(3) Class references always point to a Class object. Class reference indicates the exact type of the object it points to, and this object is an object of the Class.
2. forName ()
(1) Obtain a reference to the Class object, but the referenced Class has not been loaded (the first object of the Class has not been generated.
(2) In order to generate Class references, forName () is initialized immediately.
3. Object-getClass () gets a reference to the Class Object and returns a Class reference of the actual type of the Object.
4. getName () gets the fully qualified class name (including the package name), that is, the complete name of the class.
5. getSimpleName () Get the class name (excluding the package name)
6. getCanonicalName () gets the fully qualified class name (including the package name)
7. isInterface () determines whether the Class object represents an Interface
8. getInterface () returns the Class object, indicating all interfaces implemented by the Class referenced by the Class object.
9. getSupercalss () returns the Class object, indicating the direct base Class inherited by the Class referenced by the Class object. Using this method, you can find the complete inheritance structure of an object at runtime.
10. newInstance () returns an Oject object, which is a way to implement the "virtual constructor. "Virtual constructor": I don't know your exact type, but you must create yourself correctly in any case. The class created using this method must have the default constructor.
11. cast () accepts an object as a parameter and converts it to a Class reference type. This method is generally used when normal transformation cannot be used.
12. getClassLoader () returns the class loader of the class.
13. getComponentType () indicates the Class of the Array Component type.
14. isArray () determines whether the Class object represents an array Class.
15. generalized Class reference
1) Implementation Method: Use the wildcard "?".
2) Class <?> Better than Class, even if they are equivalent.
3) Class <?> The advantage is to explicitly tell the compiler that you have selected a non-specific class version, rather than using a non-specific class reference due to coincidence or negligence.
4) create a range: Create a Class reference so that it is limited to a certain type <className>; or any child type of this type, <? Extends superClass>; or this type of superClass, <? Super sunClassName>