Java Advanced--chapter 1 The principle of cross-platform features of Java

Source: Internet
Author: User

the cross-platform nature of Java and why java has this feature

Note: The relevant content from the network, forget the source, it is not noted, if there is improper forgetting its informed, to correct;

have a foundation Java Knowledge developers know that Java is cross-platform, but we know why Java is cross-platform,Java How is the cross-platform implemented? Let's take a look at the following.

What is a 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 human,thatCPUis the human brain, which is responsible for the thinking of the operation, but also responsible for the body parts of the command control. CPUA lot of different kinds, except for our familiarIntelwith theAMDand, as mentioned above,SUNof theSparc,likeIBMof thePowerPCand so on, these various companies produce theCPUuse 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 a streamlined instruction set (Risc) and complex instruction sets(CISC), each ofCPUhave their specific set of instructions. To develop a program, first know what the programCPUrunning on,that is to knowCPUthe set of instructions used.

The operating system is the interface software that acts as an interaction between the user and the computer, and different operating systems support differentCpu, strictly speaking, different operating systems support differentCPUset of instructions. For exampleWindowsand theLiunxall supportInteland theAMDcomplex instruction set, but does not supportPowerPCThe simplified instruction set used, and the earlierMACthe computer is usingPowerPCprocessor, so there is noMACunder direct installationwindows,until toyearsMACinstead ofIntelof theCPU,only to make theMACunder InstallationWindowsbecome possible. But the problem came, the originalMACThe operating system also supports onlyPowerPC,in theIntelcan not be installed on, how to do? So Apple has to rewrite its ownMACoperating system to support this change. In conclusion, we want to know that different operating systems support differentCPUinstruction set, now theWindows,liunx,mac,solarisall supportIntelwith theAMDof theCPUinstruction set.

with the cushion above, below tell everyone that if you are developing a program, you should first determine:

1,CPU type, that is, 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.

know what the platform is and we look at Java Cross-platform principle.

the principle of Java cross-platform

first look at a picture with C language -related diagrams:  

If you have everCdevelopment experience, this picture will look very easy. We know that as long as the standardCdevelopment of the program, using a different compilercompilers: Translating one language specification into another language specification, usually compilers translate language specifications that are easy to understand into machine-understandable language specifications. For example, we willCLanguage Specification of languages translated into platform language specification(a set of instructions that can be identified by the platform), this isCWhat the language compiler is doing "The compiled executable can be run on the corresponding platform, such asWindowscan useVCcompile, the compiledEXEthe file can beWindowsunder Operation;LiunxYou can use theGCCcompiled, the resulting executable file can beLiunxrun on. Here's a question for you to consider:"VCcompiled byEXEcan be inLiunxrunning on it? "

The answer is definitely 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-related . 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: than the If Mars really has aliens (and undoubtedly, Mars is Korean, Mars also must be invented by Koreans), as we observe ants, The Martians observe us silently, and one day, when human beings do the Your simple encounter with The hateful gold-digger ), so decided to come to Earth to educate us, but there was a question The Martians will only say Martian text, the Earth people can not understand, how to do ah? Look for translation (perhaps non-mainstream can help, jokes)! Translated the Martian text into Chinese by the Chinese translation, the English translation of the Martian text translated into English

in the above 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. then think of another question , " How do you get the Martian text across the platform?" "

The Martians had thought of the world on Earth, so they first translated their articles into Esperanto; Esperanto people of course do not understand, it doesn't matter, the Martians have also provided each country with a Esperanto-to-local translation, which Once translated (translated into Esperanto), the Martian text 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:

V First,the C language is compiled and executed, the compiler is related to the platform, and the resulting executable file is related to the platform

V Second,Java is interpreted to execute, compiled into the intermediate code of the compiler and platform-independent, compiled generated intermediate code is also platform-independent (compile, run everywhere), the intermediate code is interpreted by the interpreter execution, the interpreter is platform-related, That is, different platforms require different interpreters .

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

V First is the implementation of the compilation, as mentioned in the above , C, it is the source program from a specific platform compiler to compile a one-time platform-related machine code , Its advantage is the execution speed, the disadvantage is not cross-platform;

V Second is the interpretation of execution, such as HTML,JavaScript, which uses a specific interpreter, the line of code is interpreted as a machine code, similar to simultaneous translation, its advantages are can cross-platform, the disadvantage is the slow execution, exposing the source program;

V The third is the introduction of the " Middle Code + virtual machine " from Java , It combines the advantages of both compiled and interpreted languages, as well as the problem of traditional language headaches such as garbage collection, security checks, and so on, so Microsoft's . NET platform is also used in this way.

Advantages of cross-platform

from above, we know.JavaThe cross-platform principle is that the last generated platform-operated program files are different for different platforms, Javasource files are compiled to generate bytecode files. class,byte-code files are passed throughJVMexplain how to generate machine language that can be executed by the platform;in this process we pay attention to the wordsection code files are the same,The difference is the machine code after the explanation., Javacross-platform is based on interpretation. Classfile for the virtual machine.,but virtual machines are not cross-platform.,as long as the need to runJavaapplication's operating system, first install aJavaVirtual Machines(JVMJavaVirtual machine)can be. byJvmto be responsibleJavaThe operation of the program in the system. In simple terms, the program code is compiled and converted to a type calledJavathe middle language of the byte code,JavaVirtual Machines (JVMThe bytecode is interpreted and run. Compilation occurs only once, and interpretation occurs every time the program is run. The compiled bytecode takes aJVMThe optimized machine code form is saved, the virtual machine interprets the bytecode as the machine code and then runs on the computer.

  Java is first compiled and then explained

The same . class file gets different machine instructions (different machine instructions forWindows and Linux ) at different virtual opportunities. But the result of the final execution is the same.

cross-platform makes Java 's source code is compiled to implement its "write Once,run Anywhere" with the help of a JVM running around, but as Java b/S in the wide use of cross-platform features appear to be some chicken, this is because b/S We use the browser to the other end of the network server access, At this point the corresponding client is only dependent on the browser, so the platform corresponds to just the server platform, so its cross-platform also can not talk about.

Java Advanced--chapter 1 The principle of cross-platform features 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.