Why is the Java language platform-independent?

Source: Internet
Author: User

Properly tidied up a bit:

I. Platform and machine instructions

Regardless of which programming language is written in the application needs to go through the operating system and the processor to complete the program, so the platform is the OS and CPU composition , so-called platform-independent refers to the operation of the software is not due to the operating system, The processor has changed and is not running or running errors have occurred.
Each platform has its own unique machine instructions , the so-called platform machine instruction is can be directly recognized by the platform, the execution of a sequence of 0 and 1 code. machine instructions may be different for the same CPU and the platform that the different OS forms

Second, what is related to the platform?

C + + programs are platform-dependent: the specific platform on which the C + + source program compiles, links, and generates machine instructions based on the machine instructions of the current platform , so it can run the executable on any platform that is the same as the current platform. However, it is not guaranteed that the executable files produced by C + + source programs will run correctly on all platforms because different platforms may have different machine instructions. Therefore, once the platform has been replaced, it may be necessary to modify the source program and recompile the source program for the new platform, which is related to the platform .
The root cause of the C + + program's inability to cross the platform is that the compiler for the C + + source program compiles for the platform, and different platforms have different machine instructions, so they can't cross the platform.

Third, why Java can cross-platform

       C + + is not cross-platform, that is, because C + + source program is compiled on the platform, so closely related to the platform. If you have an environment where it can be backwards compatible (platform compatible) and up-to-the-go (Programs can run), then you can cross the platform. Based on this principle, Java provides a Java Runtime Environment--jre (installation JDK) on the computer's operating system.   The
      jre consists of a Java virtual machine, a class library, and some core files, which means that Java-written software can run as long as the platform provides a Java runtime environment.  
       C + +, the compiler provided by the Java language is not programmed for specific operating systems and CPU chips. Instead, the Java source program is compiled for the Java Virtual machine into "intermediate code" called bytecode, and then the Java Virtual Machine is responsible for translating bytecode into the machine code of the platform on which the virtual machine resides and let the current platform run the machine code.  
How the Java language works:  
A bytecode file compiled on a computer (that is, a. class file, a byte-code file is a file that has been preprocessed by a compiler, is a Java executable file exists as a binary file, but not directly executed by the system, but requires a virtual machine explanation execution . ), you can copy it directly to any computer that has a Java running environment installed. The bytecode file is interpreted by the virtual machine, i.e. the Java Virtual machine translates the bytecode into the machine code of the local computer and then gives the machine code to the local operating system to run.

Because bytecode files compiled on a computer can be copied directly to any computer that has a Java runtime installed, bytecode is actually an "intermediate code", which is why "the Java language can compile and run everywhere, which is why Java is a cross-platform." "So Java is a" semi-compiled, semi-explanatory language, "that is, the source program--and compile-and-byte code interpreter----the machine language of the corresponding platform. ”

http://blog.csdn.net/newmemory/article/details/54949817

Why is the Java language platform-independent?

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.