Question20180105 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.

1. What is a platform

Java is a cross-platform programming language, we first need to know what is the platform, Java is a high-level development language, through the interface provided by the operating system to develop, so the platform here refers to the operating system .

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 To tell everyone, if you want to develop a program, you should first determine what operating system you use, know what the platform, we look Java Cross-platform principle.

the principle of 2.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 compiler is platform-dependent and the compiled file is platform-related If Mars really has aliens (and there is no doubt that Mars is Korean, the Martian text must have been invented by Koreans), as we observe ants, The Martians observe us silently, one day, when human beings do the and so on and so on, but so the question came, Chinese translation of something only the Chinese can understand, the American Frenchman simply does not understand, The Chinese do not understand that the language cannot cross the platform.

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:

    • First, C language is compiled, compiler-related, compiler-generated executables are platform-dependent
    • Second, Java is interpreted to execute, compiled into the intermediate code of the compiler is independent of the platform, the intermediate code generated by the compiler is also independent of the platform (once compiled, run everywhere), the intermediate code is interpreted by the interpreter execution, the interpreter is platform-related, that is, different platforms need different interpreters .

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

    • First is the compilation execution, as mentioned above in the c
    • second is to interpret execution, such as Html,javascript It uses a specific interpreter to interpret the line of code as a machine code, similar to simultaneous translation, its advantage is that it can cross the platform, the disadvantage is the slow execution speed, exposing the source program;
    • The third is from java began to introduce the Span style= "FONT-FAMILY:CALIBRI;" > " median + virtual machine " It combines the advantages of both compiled and interpreted languages, At the same time, such as virtual machine can solve such as garbage collection, security checks and other traditional language headaches, so Microsoft's .net platform is also used in this way.
3. 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 the JVM, but with Java in the b/S , cross-platform features are a bit of a chicken, because b/S we use the browser to the other end of the network server access, at this time the corresponding client is only dependent on the browser, so the platform corresponds to just the server platform, so from the client to see its cross-platform is very "chicken", In fact, this idea is very biased. Let's talk about Java cross-platform chicken ribs.

4. Talk about "Chicken" Java cross-platform

  We beginner Java, I am afraid the deepest impression lies in the Java cross-platform, our predecessors teachers, will always be the Java cross-platform praise, can actually contact the development of a period of time, will there be such a question? That is, Java originates from C, and how does C work? What progress has the Java cross-platform made on the basis of C? Why is there a saying on the internet that Java's cross-platform is "chicken ribs"? Talk about the questions below;

C language is a cross-platform, but "source-level" cross-platform. There is a different compiler for Linux and Windows. So there is the C language "write once, compile everywhere", Java is "compile once, run everywhere" argument. As C + + founder Sour, the JVM has created a new platform for all Java programs to run on this platform, while C and C + + code can run on dozens of different platforms, from the source point of view, C and C + + are cross-platform, and Java is not. The source code described here has been written and never compiled. So does it look like Java's cross-platform is really a bit of a chicken?

This is also biased, because Java and C are different in the direction of the cross-platform responsibility is different, C is mainly used for system development, such as operating system : Linux, hardware drivers. So for cross-platform is the source level. And what about Java?

The origin of Java, the original intention of the invention, the beginning of Java is for the set-top box development, set-top box with the CPU is not x86 architecture of the Intel CPU but SCM, and the development of single-chip microcomputer is very rapid, you spent half a year for some kind of microcontroller developed a program, And so the program came out maybe this model of single-chip computer has no one to use, sun was to solve this problem, put forward the program only write it once again, and then let the JVM to adapt to the microcontroller model of the different. This is the first cross-platform, and the development of today? We know that Java has become the mainstream language of web development, but we developed it under Windows, and the deployment is often deployed under Linux and UNIX, so this is where Java's cross-platform can go, because whenever Java is all running on the JVM, the real need to cross-platform is the JVM, so for us to write programs, let the coding and cross-platform separation, facilitate the development.

We as the writing end, only focus on writing code, as for the cross-platform task to the JVM to handle, the intermediate code is cross-platform, but the JVM is not cross-platform, this should pay attention!

We say that the Java cross-platform chicken, is very one-sided, targeted different, no comparable!

Question20180105 the principle of cross-platform features of Java

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.