Java code execution mechanism (Distributed Java application notes)

Source: Internet
Author: User

First, let's look at the standard structure defined by the JVM specification:

First, let's look at how class files are compiled and generated. It can be seen that javac Compilation Code There are three steps. Next we will analyze the three steps: 1. The process of analyzing and inputting the parse into the symbol table (parse and enter) Parse is lexical and syntax analysis. The lexical analysis completes the conversion of the code string into the token sequence. The syntax analysis completes the generation of the abstract syntax tree from the token Sequence Based on the syntax. Enter the symbol to the symbol table. Generally, this includes determining the super type and interface of the class. Add the default constructor as needed to input the symbols in the class to its own symbol table. 2. The annotation processing step is used to process custom annotation. 3. Semantic Analysis and generation of class file (analyse and generate) The analyse step performs a series of Semantic Analysis Based on the abstract syntax tree, including the elements and variables and methods in the syntax tree, such as names and expressions, type and so on. Convert the generic type to Java. eliminate useless code (if (false ){...}); inspection and so on. Generate the class file through (COM. Sun. Tools. javac. JVM. Gen. The class file contains the structure information (including the version number of the class file format and the quantity and size of each part), metadata (corresponding to the declaration and constant information), and method information. Class loading mechanism: Loading, linking, and initialization. Then let's look at the class loading mechanism. Load loads binary bytecode into JVM. Link is responsible for verifying the format of binary bytecode, initializing static variables in the loading class, and calling interfaces and classes in the parsing class. The static initialization code, constructor code, and static property code in the initialize (initialization) Execution class will be executed in violation of the following 4 conditions: 1. new2 is called, reflection calls method 3 in the class, subclass calls initialization 4, and the initialization class specified during JVM startup. About how to use the class loading mechanism Article . Class execution mechanism: During the source code compilation stage, the source code is compiled into the JVM bytecode. The JVM bytecode is an intermediate code method, which must be interpreted and executed by the JVM at runtime, this method is called the bytecode interpretation execution method. Compilation and execution: the efficiency of interpretation is relatively low. JDK supports compiling bytecode into machine code. Compile In the runtime city, usually called JIT compiler. Compilation of code with high execution frequency, and interpretation of code with low execution frequency, is also called hotspot VM. Reflection execution: reflect, you know. It should be noted that the getmethod method has excellent performance. On the one hand, it is because of permission verification, on the other hand, scanning all methods and copying method objects, therefore, the method object returned by getmethod should be cached in the system that uses multiple reflection calls. The performance of method. Invoke is only a little lower than that of direct call.

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.