Java cross-platform Principle

Source: Internet
Author: User

This blog post is mainly from the skysky blog of xiaozhen:Http://xiaozhen1900.blog.163.com/blog/static/1741732572011325111945246/

1. Is it a platform?

Java is a cross-platform programming language. First, we need to know what platform is,We call the CPU processor and operating system as a platform..

CPU everyone knows that if a computer is a human, the CPU is the human brain. It is responsible for both thinking and command control of various parts of the body. There are many types of CPU, except for the well-known Intel and AMD, as well as the SUN's iSCSI mentioned above, such as IBM's PowerPC, etc, these companies produce CPU usage or the same or different instruction sets. The instruction set is a set of commands used in the cpu to calculate and control computer systems. The instruction sets can be further divided into simplified instruction sets and complex instruction sets (CISC ),Each typeCpuAll have their specific instruction sets.To develop a program, you must first know the CPU on which the program runs, that is, the instruction set used by the CPU.

The operating system serves as the interface software for interaction between users and computers. Different Operating Systems Support different CPUs.Different Operating Systems Support instruction sets of different CPUs.For example, windows and liunx both support Intel and AMD complex instruction sets, but do not support the simplified instruction sets used by PowerPC. Earlier MAC computers used PowerPC processors, therefore, windows cannot be directly installed on MAC. It is possible to install windows on MAC only when Intel CPU is used on MAC in. But the problem arises. The original MAC operating system only supports PowerPC and cannot be installed on Intel. What should I do? So Apple has to rewrite its MAC operating system to support this change. Finally, we need to know that different operating systems support different CPU instruction sets. Currently, windows, liunx, mac, and solaris support Intel and amd cpu instruction sets.

With the above preparations, Miss Wang will tell you that if you want to develop a program, you should first determine: 1, CPU type, that is, instruction set type; 2, operating system; we call this software and hardware combination a platform. You can also say"Platform = CPU + OS".Because the current mainstream operating systems support the mainstream CPU, the operating system is sometimes called a platform.

We know what a platform is. Let's look at the principles of Java cross-platform.

2. Principles of cross-platform Java

First, let's look at a graph related to the C language:

    

If you have had C development experience, this figure looks very easy. We know that, as long as the program is developed using standard C, the executable files compiled using different compilers can run on the corresponding platform. For example, windows can use VC to compile, then the compiled exe file can be run in windows; the GCC compilation can be used in liunx, And the generated executable file can be run on Liunx.

Here, let us think about the question: "Can a VC-compiled exe run on Liunx ?"

The answer must be no.A program compiled using a specific compiler can only run on the corresponding platform. Here we can say that the compiler is platform-related, and the compiled files are also platform-related.What we saidCross-platform programming is the cross-platform compilation of compiled files, rather than the source program.If it is a source program, any language is a cross-platform language. If you do not understand this, refer to the following case:

For example, if Mars really has aliens (and there is no doubt that Mars is from South Korea, and Mars is from South Korea), like we observe ant, the Martian silently observes us, one day, when humans do things that make the Martian really unable to read (for example, if the book published by trademanager doesn't matter if you don't buy it, haha, it's okay to vomit, so I decided to come to Earth to educate us, but there was a question: the Martian only speaks about Mars, and the Earth cannot understand it. What should I do? Ask for translation (maybe not mainstream can help, joke )! The Chinese translation translates the Mars into Chinese, the English translation translates the Mars into English, and so on. However, the question is that the Chinese translation can only be understood by Chinese people, americans and French do not understand either the English translation or the Chinese do not understand, that is, the language cannot be cross-platform.

In the above example, the Mars text is the C language, the countries are the platforms, the English translation is the compiler of the corresponding platform, and the compiled articles are executable files. Although the source article Mars has nothing to do with the platform, the translator is related to specific countries, and the translated articles are also related to specific countries.

Next, let's think about another question: "How to make Mars cross-platform ?"

The Martian thought that there was a saying in the earth, so he first translated his article into a saying in the world. Of course, there is no way to understand the saying in the world, the Martian has assigned every country a translation of the world language to the local language. This mars text can be translated once (translated as the world language) and then run in every country. Remember that in this process, the Martian team should provide two components: the first is the translation from the Mars to the world language, and the second is the translation from the world language to the corresponding local language. For example:

    

With the accumulation of the above cases, we also know the principle of cross-platform language: "You cannot compile it into a machine language, because it is related to the platform and compiled into an intermediate language, second compilation by the interpreter to explain the execution." The following figure shows the principles of cross-platform Java:

    

In. java is the source program, similar to c. c. The generated intermediate code is. class. This is the intermediate language we mentioned above, and each platform interpreter is a variety of national translations.

Next we will compare the differences between the two methods: first, the C language is compiled and executed, the compiler is related to the platform, and the executable files generated by compilation are related to the platform; second, java is interpreted and executed. The compiler that compiles the intermediate code has nothing to do with the platform, and the intermediate code generated by the compilation is also irrelevant to the platform (one compilation, running everywhere). The intermediate code is then interpreted and executed by the interpreter, the interpreter is related to the platform, that is, different platforms need different interpreters.

Next, let's talk about different types of languages based on different execution methods. The first is compilation and execution. As mentioned in C above, it compiles the source program from the compiler of a specific platform into the machine code related to the platform at one time, its advantage is that the execution speed is fast, and its disadvantage is that it cannot be cross-platform. The second is interpreted execution, such as HTML and JavaScript. It uses a specific interpreter to interpret a line of code as a machine code, similar to simultaneous translation, it has the advantage that it can be cross-platform. Its disadvantage is that the execution speed is slow and the source program is exposed. The third method is the "intermediate code + Virtual Machine" method introduced from Java, it not only integrates the advantages of the compilation language and the interpretation language, but also solves the headaches of traditional languages such as garbage collection and security check. NET platform also uses this method.

 Compile Java first and then explain

The same. class file gets different machine commands in different virtual opportunities (different machine commands for Windows and Linux), but the final execution result is the same

 

 

 

 

 

 

 

 

 

 

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.