Compiling and running Java programs

        Java is an object-oriented language and one of the best languages ​​in the world today;

        The default compilation result of Java source code is not a local machine instruction, but a bytecode with platform versatility. Therefore, Java is a language that is compiled once and runs everywhere; the bytecode file compiled from the Java source code is Running in the Java virtual machine (jvm), although the implementation mechanism of the Java virtual machine on different platforms is different, the compiled bytecode is the same; therefore, Java is a cross-platform language, and can Solve program security issues and cross-platform portability issues;

       The core of Java technology is the JVM. The JVM is an abstract computer defined by a set of specifications. The main task of the JVM is to load bytecodes into it and interpret/compile them into machine instructions on the corresponding platform. The internal virtual machine generally includes a compiler and an interpreter. The reason for this design is that through the cooperative work of the interpreter and the compiler, it can not only ensure the response time of the program, but also improve the execution performance of the program, and improve the work of the compiler. The pressure also reduces the load to a certain extent; this way, when the virtual machine starts, the interpreter can play its role first, without having to wait for the compiler to complete the compilation before execution, which can save a lot of unnecessary compilation time, and with the With the passage of program running time, the compiler gradually plays a role, according to the hot spot detection function, compiling valuable bytecodes into machine instructions in exchange for higher program execution efficiency;

            This is the end of the popularization of knowledge; there are three aspects I want to talk about next:

      1. General explanation of Java program compilation and operation

      2. The principle of compiling Java programs

      3. Running the Java program

 

 

1. Overview of compiling and running Java programs

       In order to improve the speed of writing code, each of us has our own compiler. When we finish running a program, an extra target (or possibly others) will be displayed in our compiler folder. The directory structure under this package is the same as that of our main program package, except that each .java becomes a corresponding .class file; these files are the bytecodes compiled from the source program we mentioned at the beginning file, the role of the compiler as I understand it is to give programmers code hints and the role of compiling source code. Of course, Java also has its own compiler;

        We all know that Java runs on the JVM. In fact, what runs on the JVM is the compiled bytecode file of Java. The above mentioned that our JVM is an abstract computer, so where is the location of the JVM? How does the JVM handle multiple programs? My understanding is this: Most Java virtual machine books say that the JVM is under the jre package, so we can say that the JVM reflects us as a bunch of code; when we run the program, we call it, The program we are currently running will create a virtual machine instance, and then make various allocations; we all know that there is only one virtual machine, and every time the code must be run in the same virtual machine; what about the virtual machine? deal with it? My thoughts on this piece are like this. Java is an object-oriented language. In the process of writing code, when we need a certain object, we will create a new one. Then I think this idea is the same for virtual machines. , every time we run a complete Java program

, our virtual machine will create a new virtual machine instance; the virtual machine does not have a life cycle, but these virtual machine instances have a life cycle. When our bytecode file starts to run, a virtual machine instance of this program is created , when the program ends, the virtual machine instance dies;

 

Second, the Java program bytecode compilation principle

         The javac compiler compiles Java code into a valid bytecode file through four main steps: lexical parsing, syntax parsing, semantic parsing, and generating bytecode;

         ① Lexical analysis:

          Convert the keywords and identifiers in the Java source code into a Taken sequence that conforms to the Java grammar specification, and then perform matching and verification according to the specified sequence rules. This is lexical analysis; the Taken sequence is a set of words corresponding to the source code character set. Sequence, simply put, the Taken sequence is an enumeration type, which defines many enumeration constants that conform to the Java syntax specification and correspond to the source character set,

          ②Grammar analysis:

           Refers to integrating the lexically parsed Taken sequence into a structured syntax tree;
          ③Semantic parsing

           The syntax tree generated by the previous syntax parsing step is expanded to be more complete, and the subsequent compiler will use the syntax tree after semantic parsing to directly generate bytecode;

           ④ Generate bytecode

            Call the compile() method to compile; the compile() method itself does not have the ability to perform compilation, but calls other compilation methods at runtime to finally complete the bytecode compilation;

 

 

Third, the operation of the Java program

      Valid bytecode files compiled from Java source code are run in jvm (see 1); 

      life cycle of jvm

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327056839&siteId=291194637