2.7 Compiled and interpreted

2.7 Compiled and interpreted

Earlier we used the java and javac commands to convert Hello, World! output in the console. Then why output, here we need to master two knowledge points. Compiled and interpreted languages. High-level programming languages ​​in computers are divided into compiled languages ​​and interpreted languages. And our Java has both compiled and interpreted features. From a literal point of view, compiling and explaining both have the meaning of translation. The difference between them lies in the timing . For example, a Chinese and an American have different texts. If they want to read the same book Books, such as Romance of the Three Kingdoms, if Americans want to read, we can use two methods

  1. Translate the entire Romance of the Three Kingdoms into English so that Americans can read it.
  2. The Americans found a translator, and the American wanted to read a passage, and the translator would translate a passage for him, and he could always follow the progress of the translator. But if the translator is not working that day, or the Americans want to go back and read the previous chapters, the translator needs to go back and re-translate.

These two methods can make Americans read our Romance of the Three Kingdoms. Of course, these two methods have their own advantages. The first method is to change the entire book from Chinese to English, so that foreigners can finish reading it, but if The author has updated the book, or changed the content. At this time, if foreigners want to read it, they need to re-translate the book. At this time, foreigners need to pay the same price to buy it again. In this way, the cost is Larger ones, and the second method has a translator translate in real time. If the book is changed, the translator can translate it immediately, which can save some costs, so these two methods have their own advantages.

Let's go back to compiled and interpreted

The compiled type is similar to directly translating a Chinese book into an English version for sale. In the program, we feel that it has a program responsible for translation, which converts the source code we write into a code that can be executed by a computer. The professional point of this process is called compiling or complie. This book needs to be translated or compiled from Chinese to English. Whether it is a person, a translator or a program, they need to translate or compile this code, and they are responsible in the program. compiled program, we call it a compiler

Interpretation type, which means that I say something and you explain it, and I write a piece of code and you explain it. In this way, the performance of the program may suffer a little loss, because it takes a little bit to compile, and if I want to look back at something, you have to give me a new one. translate

The compiled and interpreted languages ​​have their own advantages and disadvantages. The compiled type directly translates the entire book, and foreigners can read it directly. In this way, the execution speed will be faster, because he has translated everything Yes, the computer can only be used for execution, and the requirements for the operating system of the computer are relatively low. For example, the operating systems Windows, Linux, and macOS we develop are all written in compiled languages, such as C language and C++. languages, if we want to run the programs they write, we need to compile the code. The interpretive type is like the web pages and some scripts of the server that we usually see. It does not require high speed, so we can use this interpretive language, that is, explain while executing. For example, we are now Learning Java, of course, Java is a combination of the two. In addition to Java, there is also a scripting language JavaScript, VBscript, Python, etc., which are all interpreted languages. Although the Java language is relatively close to the characteristics of interpreted languages, we run our .java The file generates a .class file, which undergoes a compilation process in the middle, which turns the source file of javac into a bytecode file through the Java compiler, and the file that is actually executed is the class bytecode file . So we can understand that the java file has been precompiled once before execution, and the generated code is a bytecode file between the machine code and the java source code. As shown in the figure program control
, when the bytecode file is running, it will go to the JVM virtual machine, and he will put the .class bytecode file into the class loader of our virtual machine, so that this class will be loaded into our JVM Yes, after loading, it will go to the bytecode verifier. It is mainly responsible for checking whether your code is correct. If the code is wrong, it will tell you that there is a problem with your code. If the bytecode verifier If there is no problem in the test, the code is interpreted to the operating system through the interpreter, run step by step, and explain step by step, so that the program starts to run.

With the development of technology and hardware, the concept of compiling and interpreting has become more and more blurred, just like the current computer starts with 16G memory, so the efficiency of interpreting is also very high now, because our hardware has already Improve, but what we need to know here is that Java is first compiled into the operating system for interpretation, so here we need to know that Java has the characteristics of compilation and interpretation.

Just now we have talked about the operating mechanism of the program. The source file of our Java program is to compile our Java file into our class file through a javac command, and tell the operating system what we write this code through the interpreter. , the operating system operates according to our instructions, which means that the program executes a series of instructions according to regular operations. This is just a general explanation. Here we need to understand the principles, at least we need to know the process of program operation .

Guess you like

Origin blog.csdn.net/nytcjsjboss/article/details/130981251