Java core-Reflection (1), java core-Reflection

Source: Internet
Author: User

Java core-Reflection (1), java core-Reflection
JVM and class:Run java: java class with the main method to start jvm, and load the bytecode --------------------------------------------- when you call the java command to run a java program, the command will start a jvm process, all the threads and variables in the same jvm are in the same process. When the memory area of the jvm is shared, the JVM exits when the following conditions occur: (1) normal execution of the program ends (2) use system. out. exit (0) method exit (3) When an exception occurs, no exception is caught (4) the platform forces to end the JVM process. Once the JVM process ends, the memory data in the process will be lost.
1. class loading mechanism:When a Program actively uses a class, if the class has not been loaded into the memory, the system will initialize the class by loading, linking, and initializing the class. (1) class loading: When a class is loaded, the class file of the class is loaded into the memory and a java is created for it. lang. class object, which is called the bytecode File Class, is loaded by the Class loader. The Class loader is usually provided by JVM, which is called the system Class loader, we can also inherit the ClassLoader class to provide a custom class loader. different class loaders can load local bytecode files, the bytecode in the jar package, and the link of the (2) Class loaded through the network: after the class is loaded into the memory, the system generates a corresponding Class object for it, and thenMerge binary data into JRE1> Verification: Check whether the loaded class has a correct internal structure 2> preparation: assign a value to the static variable of the class to allocate memory, and set the default value 3> Resolution: replace the symbolic reference in the binary data of the class with the direct reference (3) class initialization at this stage, JVM is responsible for class initialization, the initialization class of static variables includes the following steps: 1> If the class has not been loaded or linked, the program first loads and links the Class 2> If the direct parent class of the class has not been initialized, it first initializes its parent class. 3> if there are initialization statements (static code blocks) in the class, the system will execute these initialization statements in sequence ------------- symbol reference: Symbol reference is a string, it gives the name of the referenced content and may contain some other information about the referenced item-this information must be unique enough to represent a class, field, in this way, the full name of the class must be given for symbol references of other classes.
2. Reflection1. Reflection ComprehensionMetadata: description of data description data reflection: obtains the metadata of a class during runtime, dynamically obtains the member information (constructor, method, field, internal class, interface, parent class, and other methods) and describe each member of the class into a new class.
Class class: used to describe the type of a class or interface. instance of the Class class that describes the Class: class instance indicates the class or interface in the JVM (one byte code ), enumeration is a special class, and annotation is a special interface. When the program uses a java. util. Date class for the first time, it loads the bytecode of the modified class into the VM and creates a class object. The class Object indicatesJava. util. DateThe class can represent n bytecode objects. How does one differentiate the class's bytecode at this time? To solve this problem, the class designer proposed generic -->Class <String>
Java. lang. string bytecode type: class <java. lang. string> java. util. date bytecode type: class <java. util. date> java. util. the bytecode type of Arraylist: class <java. util. arraylist>
How to Create a class Object and express a bytecode?

// Get bytecode object: class Object public class ClassinstanceDemo {public static void main (String [] args) throws ClassNotFoundException {// get java. util. date class bytecode object // Method 1: Use the class attribute, Class name class; class <java. util. date> clz = java. util. date. class; // Method 2: Get through the getClass method of the object, which is the method java in the object. util. date date = new java. util. date (); Class <?> Clz2 = (Class <Date>) date. getClass (); // method 3: Use the static method forname (String className) class in the Class <?> Clz3 = Class. forName ("java. util. date "); System. out. println (clz); System. out. println (clz2); System. out. println (clz3 );}}


Only one bytecode file exists in the same class in JVM, that is, clz = clz2 = clz3 Third, a large number
2. Nine built-in class instances and array class instances
The basic data type cannot be expressed as an object, so the getclass method cannot be used. The basic data type does not have the concept of a Class name or use a Class. forName: How does one represent class objects of the basic data type? All data has the class Attribute class <?> Clz = data type. class1. nine built-in class instances: class instances provided in the JVM in advance: byte, short, int, long, float, double, char, void: byte. class .... void. class has a method type in the eight data types, and returns the basic data type bytecode Object System of the packaging class. out. println (Integer. TYPE = int. class) // true; 2. array class instance: array refers to the referenced data type. array is actually how the object represents the array class instance method 1: array type. class Method 2: array object. getclass; All arrays with the same dimension and element type share the same bytecode and Element does not matterClass: Describes all types, so the class should have all types of the same method object: Describes all objects, so there should be a common method of all objects in the object
Top
0
Step on
0
View comments

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.