Knowledge Point 20180506 delve deeper into Java's cross-platform features

Source: Internet
Author: User
Tags garbage collection

The topic of this chapter : from the bone to see Java cross-platform ; The contents of this article are partly excerpted from https://www.cnblogs.com/roger-yu/p/5827452.html

have a foundation Java Knowledge developers know that Java is a cross-platform, but we know why Java Cross-platform,Java Cross-platform is how to achieve it? 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 the platform,Java is a high-level development language , through the interface provided by the operating system to develop , So the platform here mainly 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 installationWindowsuntil05yearsMacinstead ofIntelof theCpuonly to make theMacunder InstallationWindowsbecome possible. But the problem came, the originalMacThe operating system also supports onlyPowerpcin 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 above cushion, we will tell you, if you want to develop a program, you should first determine your own use of the What operating system , know what the platform is, we look at the Java cross-platform principle.

The principle of 2.Java cross-platform

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

If you have a c development experience, This picture will look very easy. We know that as long as the standard c The program developed using a different compiler compiler: translating one language specification into another language specification, Often compilers translate language specifications that are easy to understand into machine-understandable language specifications. For example, we will C () c What the language compiler did. The compiled executable can be run on the corresponding platform, such as windows can use VC compile, that compiled exe file can be in windows liunx gcc compile, the resulting executable can be liunx "Vc compiled exe can be in the Liunx running on it? "

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 of language cross-platform is compiled before the file cross-platform "cross-platform into the source-level cross-platform and intermediate code cross-platform", rather than 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't see (such as your simple encounter with The hateful gold), so decided to earth education us, but there is a question , 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 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 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. Next, consider 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 translation of various countries.

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:

    • the first is the compile execution, as mentioned 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;
    • the second is to explain the implementation, such as Html,javascript, it uses a specific interpreter, the line of code is interpreted as machine code, similar to simultaneous translation, its advantages are can cross-platform, the disadvantage is the slow execution, exposing the source program;
    • The third type is from Java began to introduce the " Intermediate code + virtual machine " approach, which combines the advantages of both the compilation language and the interpretation language, as well as virtual machine can solve such as garbage collection, security checks and other traditional language headache problems, 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 platformsJavasource 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 note that the bytecode 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(JVM javavirtual 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.

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

cross-platform makes java The source code once compiled can be aided by jvm running everywhere implements its "write Once,run Anywhere" but with Java b/s B/s " ribs " , Actually, the idea is biased. . > Let's talk about this for a second. java

4. talk about " chicken " Java cross-platform

  We're beginners .Javaof the,I'm afraid the deepest impression is thatJavacross-platform.,our Senior teachers,Will always beJavaCross-platform praise.,After a period of actual exposure to development,will there be such a question??That'sJavaoriginated fromC++,thatC++How does that work?? JavaCross-platform exactly in theC++on the basis of,What progress has been made??Why is there a saying on the internet,sayJavathe cross-platform is very"Chicken Ribs"??is that so??Talk about these questions below;

C++language is a cross-platform, but "source-level" cross-platform. There was an originalLinux,Windowsthe different compilers. So there areC Language "write once, compile everywhere",Java is "compile once, run everywhere" the argument. AsC++Sour, founder of the story,JVMcreated a new platform for allJavaThe program can only run on this platform, andCand theC + +code can be run on dozens of different platforms, from a source point of view,Cand theC + +is cross-platform, andJavaNo, No. The source code described here has been written and never compiled. So look at this.Javacross-platform is not really a little chicken?

This argument is also biased, because Java and C are different in the direction, so the responsibility of cross-platform is different,C is mainly used for the development of the bottom system, such as operating system : Linux, hardware drivers. So for cross-platform is the source level. Java, however, is a cross-platform of intermediate yards.

C + + compilation generates a purely binary machine instruction, while Java compilation generates a non-pure binary byte code. This Unicode-based bytecode is not dependent on a particular computer hardware architecture-which means that your bytecode can run on any platform as long as the JVM environment is present. Second, Java bytecode binary data is stored and transmitted in a fixed format, eliminating the interference of byte order.

Why can Java cross-platform? Because bytecode is run on a virtual machine, not the compiler. In other words, because the JVM can be installed across platforms, the corresponding Java bytecode can then be run on any platform. As long as the JVM's own code can run on the appropriate platform, that is, the JVM is viable, then Java programmers can take care not to think about where the program is written, but to run it on the virtual machine and then become the platform's machine language, which is not something the programmer should be concerned about.

JavaThe Origins, the original intention of the invention, At the beginningJavais toSingle Chip microcomputerdeveloped by,and the development of single-chip microcomputer is very rapid, you've spent half a year developing programs for some kind of microcontroller., when the program comes out, it's possible that this type of microcontroller is no longer available., SunIt was to solve the problem., The program is written and compiled once., then letJVMto adapt to the different types of single-chip microcomputer.This is the first cross-platform, and the development of today? We knowJavahas become aWebdevelopment of the mainstream language, but we developed the time is inWindowsand deployments are often deployed inLinuxand theUnixunder, so at this pointJavathe cross-platform can also be a big thing, because no matter when,Javaplainly, it's all running inJVMon the, really need cross-platform isJVM, so for us to write the program, let the coding and cross-platform separate, facilitate the development and testing. The most striking point is thatJavain the InheritanceC + +the advantages of the time, also abandoned theC + +One of the drawbacks is that memory is allocated and recycled,JavaThis part of the work is handed over toJVMautomatically, and eases the developer's action on this, so sayJavathe cross-platform is caused by many factors, developed to todayJavahas gone beyond one language and become more of a platform. "Imagine if it is written as C + +, then we have to compile after the program is compiled to be able to, and through Java we can be directly compiled in the development, only need to deploy the intermediate code on the server, the following things do not need us to manage, relatively speaking, development and deployment is simple. "

We as writing end , focus on writing code JVM The middle code is a cross-platform but JVM

in the comprehensive ; we say Java of cross-platform chicken , It's very partial. , different targeting , No comparability !

Repeat

we have not considered such a question, that is the same cross-platform, why the Internet development More choice Java, without choosing C ?

actually choose which language to use for development, which does not have a completely correct answer, we use Java- developed programs can also be developed using C , except that the language requirements at the beginning of the Internet are secure, cross-platform and support internet operations, while Java Very good support for these, while Java from C + +, and the rejection of C + + bad things, so relatively Java 's simplicity has been praised by more people, the use of more people, the market for its support is also more.

Knowledge Point 20180506 delve deeper into Java's cross-platform features

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.