Computers do not understand high-level languages, and of course they cannot execute high-level languages directly. Computers can only understand machine language directly, so any language must be translated into machine language. Programs written in any programming language are ultimately executed by the machine code (01 sequence) of the underlying machine, whether it is a compiled or interpreted language. The source code for any high-level programming language program is a sequence of characters, and the sequence of characters to the underlying sequence of 01 is accomplished through multiple transformations by the compiler or parser.
Compilation vs Interpretation
There are two ways of translating: one is compiling, one is explaining.
the time of translation is different in two ways.
- Compiled language written program before being executed, need a special compilation process, the program compiled into machine language files, such as EXE files, later to run without re-translation, directly using the results of the compilation (EXE file), because the translation is done only once, the runtime does not need to translate, Therefore, the program execution of the compiled language is highly efficient, but it cannot be generalize, and the interpreter of some interpreted languages can even make the performance of the interpreted language more than the compiled language by dynamically optimizing the code at runtime. .
A complete compilation system with a program written in C hello.c the compilation process
- Interpretation is different, the interpretation of the language of the program does not need to compile, save the process, the interpretation of the language in the process of running the translation, such as the explanatory Basic language, a special interpreter can directly execute the BASIC program, each statement is executed when the translation. Such explanatory language should be translated once every time it is executed, and the efficiency is lower.
There are pros and cons to both compiled and interpreted types.
Because the program executes fast, the system requirements are lower under the same conditions, so it is used in the development of operating system, large-scale application, database system and so on, such as C/S, Pascal/object Pascal (Delphi), etc. are compiled languages.
Some Web scripts, server scripts, and auxiliary development interfaces for applications where speed requirements are low, and compatibility with different system platforms are required , often use explanatory languages such as JavaScript, VBScript, Perl, Python, Ruby, MATLAB and so on.
Interpreted language each code is only at run time and the system knows if the code is wrong (except for the Java-based explanatory language). In other words, because the compiled language is compiled before it is run, the compiler checks all the code so that there are no low-level errors, such as using a nonexistent name or using the wrong name. And JavaScript can have these problems.
However, with the upgrading of hardware and the transformation of design ideas, compiling and interpreting languages are more and more general, mainly embodied in some emerging high-level languages, and the interpretation of the language's own characteristics also makes compiler manufacturers willing to spend more cost to optimize the interpreter.
Java
The Java language is a compiled-interpreted language with both compilation and interpretation features (its so-called compilation process is simply programming. java files into platform-independent bytecode. class files, and not compiled into executable machine languages like C ). As a compiled language, Java programs are uniformly compiled into bytecode files-the file suffix is class. This type of file is also referred to as a class file in Java. Java class files can no longer be executed directly on the computer, they need to be translated into the local machine code by the Java Virtual machine, and the translation process of the Java Virtual machine is explanatory. The Java bytecode file is first loaded into the computer's memory and then read out an instruction that translates an instruction that executes an instruction that is called the Java language interpretation execution, which is done by the Java virtual machine. In reality, the Java Development tool JDK provides two very important commands to complete the compilation and interpretation (translation) process above. The two commands are Javac.exe and Java.exe, which load the Java class file and progressively compile the bytecode file, while the other command corresponds to the Java language Interpretation (Javac.exe) process. In order, the Java language is the process of compiling first and then interpreting execution.
bytecode is not designed specifically for any particular processor hardware platform corresponding to the instruction code (for example, Intel's Pentium microprocessor or IBM's system/390 processor). Bytecode is platform-independent code that can be sent to any platform and can be run on that platform. Very similar to the instruction code of the machine instruction . Therefore, the Java virtual machine can easily convert the bytecode directly to the machine code corresponding to the specific CPU, thus obtaining higher performance. This conversion is much more efficient than other explanatory languages such as Basic, Perl, and so on, even on very low-level CPUs. But Java, after all, is an explanatory language, and it explains the significance of execution by enabling cross-platform operations that can be performed on many different computers once the program is compiled. Although this is much slower than the C program, it can be accepted in most applications. And now that Java has a dedicated code generator, it's easy to use JIT-compilation techniques to convert bytecode directly into high-performance native code. It is worth mentioning that the Java Runtime system provides this feature while still having platform independence, so "efficient and cross-platform" is no longer contradictory to Java.
"Understanding bytecode and understanding how Java compilers generate Java bytecode and learning-assembler knowledge is just as meaningful for C + + programmers." ”
There are already many kinds of Java Virtual machine products, including free software and commercial software. If executing Java bytecode is not ideal among Java virtual machines, you can use some tools such as GNU Compiler for Java to compile Java code or Java bytecode into machine code and run directly by the hardware . While some processors can run Java bytecode directly, the processor is called a Java processor .
C#
The C # language is a compiled language, but its "compile" process is more specific, as follows:
The C # program relies on its. NET Frameworker platform for the first time, compiles the Il Intermediate code, and then executes the machine code that is translated by the JIT compiler to local. From the second run of the same program, you do not need to perform the above compilation and translation process, but directly run the first translated machine code. So for C #, it usually takes a long time to run for the first time, but from the second start, the execution time of the program is much faster.
So why does C # have to compile two times? In fact, Microsoft wants to implement the optimization of its program operation by dynamic compilation (implemented by JIT compiler tool). If the code is dynamically compiled before it runs, then the JIT compiler can be intelligently tuned to your local machine's hardware conditions, such as using better registers, machine instructions, and so on, instead of building a program that runs on all hardware machines, The conditions of each machine are not fully utilized.
scripting language
Note: Scripting languages generally have a corresponding scripting engine to interpret execution. They usually need an interpreter to run. Javascript,asp,php,perl are scripting languages. Compiled and linked by C/C + +, you can create an EXE file that executes independently.
JIT (JIT Compiler,just-in-time compiler, instant compiler)
The JIT compiler is able to compile MSIL into various machine codes to suit the corresponding system platform, which ultimately makes the program run smoothly in the target system.
In the Java programming language and environment, the instant compiler (JIT Compiler,just-in-time compiler) is a program that translates Java bytecode (including programs that need to be interpreted) into instructions that can be sent directly to the processor.
The JIT compiler is divided into: economic compilers and general compilers.
JVM (Java virtual Machine,java vm)
The JVM is a specification for computing devices, a fictional computer that is implemented by simulating various computer functions on a real computer.
A very important feature of the Java language is its independence from the platform. The use of Java virtual machines is the key to achieving this feature. general high-level languages if you want to run on a different platform, you need to compile at least a different target code . When the Java language virtual machine is introduced, the Java language does not need to be recompiled when it runs on different platforms. The Java language uses a Java Virtual machine to mask information related to a specific platform, allowing the Java language compiler to generate only the target code (bytecode) that runs on a Java virtual machine, which can be run unmodified on multiple platforms. When a Java virtual machine executes a bytecode, it interprets the bytecode as a machine instruction execution on a specific platform. This is why Java can be " compiled once and run everywhere ".
Reference links
- Differences between compiled languages, interpretive languages, and scripting languages
- Java byte code
- The difference between interpreted and compiled languages
- JIT compiler
- Jvm
Problem
Now that Java has a dedicated code generator, it is easy to use JIT-compilation techniques to convert bytecode directly into high-performance native code. It is worth mentioning that the Java Runtime system provides this feature while still having platform independence, so "efficient and cross-platform" is no longer contradictory to Java.
The JIT compiler technique here is to use different JIT compilers on different platforms to translate. Class into the corresponding native code, like a JVM? Do you need to translate it again next time? Or is it possible to run it directly in the future?
Compiler Language & Explanatory language