This blog post is mainly from ZERO516CN and the Web xiaozhen of the Sky :http://xiaozhen1900.blog.163.com/blog/static/ 1741732572011325111945246/
1, what is the platform
Java is a cross-platform programming language, so we first need to know what the platform is, we call the CPU processor and the overall operating system as a whole platform .
CpuWe all know that if the computer is humanThatThe CPU is the human brain, which is responsible for the thinking operation, but also responsible for the body parts of the command control.There are many kinds of CPUs, except for our familiarIntel andOutside of AMD, as mentioned aboveSun'sSparc, such as IBM's PowerPC, and so on, these companies produce CPU usage or the same or different instruction sets. An instruction set is a set of instructions that the CPU uses to compute and control a computer system. The instruction set is also divided into thin instruction set (RISC) and complex instruction set (CISC), each CPU has its own specific instruction set. to develop a program, you first know what CPU the program runs on, that is , the set of instructions that the CPU uses .
The operating system below says,The operating system is the interface software that acts as an interaction between the user and the computer, and different operating systems support differentCPU, in the strictest sense.different operating systems support different sets of instructions for the CPU. For exampleWindows andLiunx all supportIntel andAMD's complex instruction set, but does not supportPowerpcThe simplified instruction set used, and the earlierMacThe computer is usingPowerpcProcessor, so there is noDirect installation under MacWindows, until 05 mac instead Span style= "-ms-word-wrap:normal;" lang= "en-US" >intel cpu to enable the Span style= "-ms-word-wrap:normal;" lang= "en-US" >mac install Windows becomes possible. But the problem came, the original mac operating system also supports only powerpc, in intel also can not be installed, how to do? So Apple has to rewrite its mac operating system to support this change. In conclusion, we want to know that different operating systems support different cpu instruction set, now windows,liunx,mac, Solaris supports both Intel and AMD's CPU instruction sets.
With the above cushion, want wants to tell everyone, if you need to develop a program, the first should be determined: 1,CPU type, that is, the instruction set type, 2, operating system; We call this combination of hardware and software platform. You can also say " platform = Cpu+os". and because the mainstream operating system now supports the mainstream CPU, it is sometimes called the operating system as a platform.
Knowing what the platform is, we look at the Java cross-platform principle.
2. The Java cross-platform principle
First look at a diagram related to the C language:
C's development experience, this picture will look very easy. We know that, as long as the standard c developed programs, using different compilers compiled executable files can be run on the corresponding platform, such as windows can use VC compile, the compiled Span style= "-ms-word-wrap:normal;" lang= "en-US" >exe files can be Run under Windows; liunx can be used GCC compiled, the resulting executable can be liunx run.
Here to ask you to consider a question: "VC compiled EXE can be run on the liunx?" "
The answer must be no. A program compiled with a specific compiler can only run on the corresponding platform, and here it can be said that the compiler is platform-dependent and the compiled file is platform-dependent. we speak the language cross-platform is the compiled file cross-platform, not the source program cross-platform , if it is the source program, any language is a cross-platform language. This if you do not understand, look at the following case:
For example, Mars really has aliens (and there is no doubt that Mars is Korean, Martian text must have been invented by Koreans), as we observe ants, Martians observe us silently, one day, when human beings do what things let the Martians really can not see it (such as the teacher's book published you do not buy, haha, Vomiting, it doesn't matter, spit ah spit ah on the habit of vomiting, so decided to Earth to educate us, but there is a problem, Mars will only say Martian text, Earth people can not understand, how to do? Look for translation (perhaps non-mainstream can help, jokes)! Translated by the Chinese translation of the Martian text into Chinese, English translation of the Martian text translated into English and so on, but such a problem, the Chinese translation of something only the Chinese can understand, the American Frenchman does not understand, the English translation of the article Chinese do not understand, that is, the language can not cross the platform.
In the example, the Martian text is C language, each country is a platform, Chinese translation English translation is the corresponding platform compiler, the compiled article is an executable file. Although the source article Martian text is platform-independent, but the translator is related to a specific country, translated articles are also related to a particular country.
Next, consider another question, "How do you get the Martian text across the platform?" ”
The Martians thought of the world on Earth, so they first translated their articles into Esperanto; Esperanto people certainly do not understand, it doesn't matter, the Martians have also provided each country with a Esperanto-to-local translation, so that the Martian text once translated (translated into Esperanto), you can go to each country to run. Also keep in mind that this process Mars will provide two components, the first is the Martian text to Esperanto translation, the second is Esperanto to correspond to the local language translation. Such as:
With the accumulation of the above cases, we also know the language cross-platform principle: " can not be compiled into machine language, because it is related to the platform , compiled into an intermediate language , and then two times by the interpreter compiler, interpretation of execution." The following is the Java cross-platform schematic diagram:
c language .c, the resulting intermediate code is . class, which is the middle language we said above, each platform interpreter is a translation of various countries.
language is compiled execution of compiler and platform related compiler generated executable file related to platform java is interpreted to execute, compiled into the intermediate code of the compiler is independent of the platform, the compilation generated by the intermediate code is also independent of the platform (compile, run everywhere), the intermediate code is interpreted by the interpreter execution, the interpreter is platform-related, that is, different platforms need different interpreters.
Here's the language. According to the different types of execution: the first is the implementation of the compiler, as mentioned in the above C, it is the source program from a specific platform compiler compiled into platform-related machine code , it has the advantage of fast execution, the disadvantage is not cross-platform; the second is interpreting execution, such as HTML , JavaScript, which uses a particular interpreter, interprets a line of code as a machine code, similar to simultaneous translation, which has the advantage of being able to cross-platform, with the disadvantage of slow execution, exposing the source program, and the third is the "middle code + virtual machine" approach introduced from Java, It combines the advantages of compiling and interpreting languages, as well as the problem of traditional language headaches such as garbage collection, security checks, and so on . NET platform is also used in this way.
Java is first compiled and then explained
The same . class file gets different machine instructions (different from Windows and Linux machine instructions) at different virtual opportunities , but the result is the same for the final execution
The cross-platform of Java