Learn more about the cross-platform of Java

Source: Internet
Author: User

The question is reproduced from: http://www.cnblogs.com/gw811/archive/2012/09/09/2677386.html

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.

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 is the interface software that acts as an interaction between the user and the computer, and different operating systems support differentcpu, in strict sense is different operating systems support different cpu instruction set. For example   windows and liunx all support intel and amd complex instruction set, but does not support powerpc used by the compact instruction set, while the earlier mac the computer using the POWERPC processor, it is not possible to install lang= >mac "en-us" windows until 05 mac instead of cpu, so that in mac under the installation windows become possible. But the problem came, the original mac  operating system also supports only powerpc, intel can not be installed, how to do? So Apple has to rewrite its mac operating system to support this change. Finally, we want to know that the different operating systems support different cpu instruction set, now windows,liunx,mac, Solaris supports both Intel and AMD's CPU instruction set .

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 the standard C development of the program, the use of different compiler compiled executable files can be run on the corresponding platform, such as windows can use VC compile, 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. If you do not understand, look at the following case: For example, Mars really has aliens (and undoubtedly, Mars is Korean, the Martian text must have been invented by Koreans), like we observe ants, the Martians silently observe us, one day, when the human What makes the Martians really can not see (such as want to be a teacher's book published you do not buy, haha, vomiting, no matter, spit, spit, spit on the habit), so decided to educate us on earth, but there is a problem, Mars people 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:

    

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 to execute, the compiler is related to the platform, the compiler generated executable file and platform-related; second, theJava is interpreted to execute, compiled to the intermediate code of the compiler is independent of the platform, the compilation generated intermediate code is not platform-independent (compile, Run everywhere), the intermediate yards are then interpreted by the interpreter, which is platform-dependent, that is, different platforms require 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.

3. Summary

From the above we know the Java cross-platform principle that the last generated platform-operated program files are different for different platforms, Java source files are compiled to generate bytecode files. Class, bytecode files are interpreted by the JVM to generate machine languages that can be executed by the platform; bytecode files are the same , unlike the interpreted machine code, which is based on the interpretation of Java cross-platform. class file, but the virtual machine is not cross-platform, just install a Java VM (JVM javavirtual Machine) on the operating system that needs to run the Java application. The JVM is responsible for the operation of the Java program in the system. In simple terms, the program code is compiled and converted into an intermediate language called Java bytecode, and the Java Virtual Machine (JVM) interprets and runs the bytecode. Compilation occurs only once, and interpretation occurs every time the program is run. The compiled bytecode is saved as a machine code optimized for the JVM, the virtual machine interprets the bytecode as a machine code, and then runs on the computer.

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

Learn more about the cross-platform of Java

Related Article

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.