Article Directory
- 1.java program compilation and running process
- 1. Java source code
- 2. Compiler
- 3. JVM executable bytecode
- 4. Interpreter in JVM
- 5. Machine-executable binary machine code
- 6. Program run
- 2. What is reflection
- 1. The relationship between reflection and bytecode
- Object workflow
- 3. Use of reflection
- 1. Get Class object
- 2. Create an object instance
- 3. Access fields
- 4. Calling methods
- 5. Operand array
1.java program compilation and running process
javasource code- - - Compiler - - - jvm executableBytecode- - - Interpreter in jvm - - - Machine executable binary machine code - - - Program running
1. Java source code
-
write: Developers use Java language to write source code and save it in
.java
in the file with the extension.
2. Compiler
-
Compilation: Java compiler (such as
javac
)Will.java
The source code in the file is compiled into Java bytecode. This step involves syntax analysis, semantic checking, generating intermediate representations and finally generating bytecodes. -
Output: The output generated by the compiler is
.class
File containing bytecode corresponding to the source code.
3. JVM executable bytecode
- Bytecode: Bytecode is an intermediate form between source code and machine code, designed to run on any platform that implements Java virtual machines.
4. In JVMInterpreter
-
load: When running Java programs, the JVM's class loader is responsible for
.class
The file is loaded into memory. - connect: During the connection phase, the JVM performs verification, preparation and parsing steps to ensure the security and correctness of the code.
- initialization: At this stage, the JVM processes static variables and static blocks and executes the class constructor.
5. Machine-executable binary machine code
- Explain execution: The JVM's interpreter interprets the execution bytecode one by one, and translates it into machine instructions that can be executed on specific hardware. This is a step-byte code instruction interpreted at a time.
- Instant Compilation (JIT Compilation): In order to improve performance, hotspot code (i.e. code with more executions) will be compiled by the instant compiler in the JVM to make local machine code, so that it can be directly executed by the hardware without explaining it one by one, thereby improving execution efficiency.
6. Program run
- implement: After all the above conversions and processing, the program is finally run on the computer hardware, and the user can see the program execution results.
2. What is reflection
Official definition: Reflection is a powerful mechanism that allows programs to query and manipulate object class information at runtime. Using the reflection API, you can dynamically create objects, call methods, access fields (even if they are defined as private), and be able to load classes.
1. The relationship between reflection and bytecode
Bytecode is the intermediate file expression of the java source code compiled by the javac compiler, that is, the contents in .class. These bytecodes can be regarded as the instruction set for execution of jvm.
Reflection is a mechanism for checking or modifying program behavior at runtime. It is based on bytecode, that is, it will first create a Class object, which can read class information from the bytecode, i.e.Constructor, methods, fields,Private access can also be accessed
, so there isDynamic operation
Advantages andDestroy encapsulation
Disadvantages.
Object workflow
-
Class loading: When the JVM first references a class, it will load the bytecode of the class, parse the metadata in the bytecode, and create a
Class
Object of type. -
Information access: By this
Class
Objects, which can access various information of the class:- Get fields(Field): All fields declared in a class can be retrieved regardless of their access rights.
- How to get it(Method): All methods defined by the class can be obtained.
- Get the constructor(Constructor): All constructors of the class can be accessed.
- Get superclasses and interfaces: You can view the parent class and the implementation interface of the class.
-
Create an instance: Can be used
Class
Object to create an instance of the class it represents. This is usually done by callingnewInstance()
Method implementation, or by obtaining the appropriate constructor object and calling itnewInstance()
method. -
Dynamic operation: Methods or fields can be called dynamically, which is unknown at compile time, so special classes are not required.
3. Use of reflection
1. Get Class object
Class<?> clazz1 = Class.forName(""); // Fully qualified name of the class
Class<?> clazz2 = ArrayList.class; // By class literal
ArrayList<String> arrayList = new ArrayList<>();
Class<?> clazz3 = arrayList.getClass(); // Call getClass() via an object instance
- 1
- 2
- 3
- 4
- 5
- 6
2. Create an object instance
Class<?> clazz = Class.forName("");
Object arrayList = clazz.getDeclaredConstructor().newInstance(); // Create an object instance
- 1
- 2
3. Access fields
Class<?> clazz = Class.forName("");
Field field = clazz.getDeclaredField("myField"); // Get the specified field
field.setAccessible(true); // Setting accessibility is very important for private fields
Object fieldValue = field.get(instance); // Read field values
field.set(instance, "new value"); // Set new field value
- 1
- 2
- 3
- 4
- 5
4. Calling methods
Class<?> clazz = Class.forName("");
Method method = clazz.getDeclaredMethod("myMethod", String.class); // Get method
method.setAccessible(true); // This is necessary for private methods
Object result = method.invoke(instance, "parameter"); // Call method
- 1
- 2
- 3
- 4
5. OperationArray
int[] intArray = (int[]) Array.newInstance(int.class, 3); // Create an integer array
Array.set(intArray, 0, 123); // Set array elements
int value = Array.getInt(intArray, 0); // Get array elements
- 1
- 2
- 3