Java Cross-platform principle

Source: Internet
Author: User

This blog post mainly originates from the web Xiaozhen of the sky: http://xiaozhen1900.blog.163.com/blog/static/1741732572011325111945246/

1, 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 .

CPU everyone knows, if the computer is human , that CPU is the human brain, it is responsible for the thinking of computing, but also responsible for the body parts of the command control.

There are many kinds of CPUs, except for the Intel and AMD we know well, and for example Sun's Sparc, such as IBM's PowerPC and so on, which each company produces CPU usage or the same or different instruction set.

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, the operating system is the interface software that acts as the interaction between the user and the computer, different operating systems support different CPUs, in a strict sense, different operating systems support different CPU instruction sets.

For example   windows and liunx all support Intel and amd, but does not support the compact instruction set used by powerpc, whereas earlier mac computers used the Span lang= "en-US" >powerpc processors, so it is not possible to install >mac windows until 05 mac Switch to intel cpu, so that mac installed windows became possible.

< Span lang= "en-US" > But the problem came, the original mac  operating system is also only supported powerpc, intel also can not install, how to do? So Apple has to rewrite its mac operating system to support this change.

Finally, we want to know that different operating systems support different CPU instruction sets, and now Windows,liunx,mac,solaris supports both Intel and AMD CPU instruction sets.

With the above bedding, want to tell everyone, if you need to develop a program, you should first determine:

1,CPU type, i.e. instruction set type;

2, operating system; We call this combination of hardware and software a 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:

    

If you have a C development experience, this picture will look very easy. We know that as long as a program developed with standard C, the compiled executables using different compilers can be run on the corresponding platform, such as

Windows can be compiled with VC, the compiled EXE file can be run under windows;

Liunx can be compiled with GCC, and the resulting executable can be run on Liunx.

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 only once translated (translated into Esperanto ), Can be run in various countries.

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:

    

Java is the source program, similar to the C language . C, the generated intermediate code is . Class, this is the middle language we said above, each platform interpreter is a variety of national translation.

Next we compare the difference between the following two ways:

First, the C language is compiled and executed, the compiler is related to the platform, and the executable file generated by the compiler is related to the platform;

Second, theJava is interpreted to execute, compiled into the intermediate code of the compiler and platform-Independent, compiled generated intermediate code is also independent of the platform (compile, run everywhere),

The intermediate code is then interpreted by the interpreter, and the interpreter is platform-dependent, that is, different platforms require different interpreters.

I'll talk about it here. The different classifications according to the way of execution:

The first is to compile the execution , as mentioned in the above, C, it is the source program by a specific platform compiler to compile a one-time platform-related machine code , its

The advantage is that the execution speed is fast,

Disadvantage is the inability to cross the platform;

The second is to interpret execution, such as Html,javascript, which uses a specific interpreter to interpret a line of code as a machine code, similar to simultaneous translation, which

The advantage is that you can cross-platform,

The disadvantage is that the execution speed is slow, exposing the source program;

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, which can be solved by virtual machines.

So after Microsoft's . NET platform is also used in this way.

 Java is first compiled and then explained

 The same . class file gets different machine instructions (different machine instructions for Windows and Linux) at different virtual opportunities.

But the result of the final execution is the same.

Java Cross-platform principle

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.