Home > Java > Javagetting Started > Why is Java language platform independent?

Why is Java language platform independent?

藏色散人
Release: 2020-05-08 14:42:02
Original
4122 people have browsed it

Why is Java language platform independent?

Why is the Java language said to be platform independent?

1. Platform and machine instructions

No matter which programming language an application is written in, it needs to go through the operating system and processor to complete the running of the program. Therefore, the platform here is composed of OS and CPU. The so-called platform independence means that the software will not fail to run or run errors due to changes in the operating system and processor.

Each platform has its own unique machine instructions. The so-called machine instructions of a platform are a sequence code composed of 0s and 1s that can be directly recognized and executed by the platform. The machine instructions of platforms formed by the same CPU and different OS may be different

Recommended tutorial: "java learning"

2. What is platform-related?

C/C programs are platform-dependent: the specific platform where the C/C source program is located compiles and links its source files and generates machine instructions, that is, generating executables based on the machine instructions of the current platform. file, so it can run this executable on any platform that is the same as the current platform. However, there is no guarantee that the executable files generated by the C/C source program can run correctly on all platforms because different platforms may have different machine instructions. Therefore, once the platform is changed, the source program may need to be modified and recompiled for the new platform, which is platform-dependent.

The fundamental reason why C/C programs cannot cross platforms is that the compiler of C/C source programs compiles for the platform, and different platforms have different machine instructions, so they cannot cross platforms.

3. Why Java can be cross-platform

The reason why C/C cannot be cross-platform is because the C/C source program is compiled for the platform, so it is not related to the platform closely related. If there is such an environment: it is both backward compatible (compatible with the platform) and upward compatible (the program can run), then wouldn't it be cross-platform? Based on this principle, Java provides a Java running environment - JRE (installation of JDK) on the computer's operating system.

JRE consists of a Java virtual machine, class libraries and some core files. That is to say, as long as the platform provides a Java running environment, software written in Java can run on it.

Different from C/C, the compiler provided by the Java language does not program for a specific operating system and CPU chip, but compiles the Java source program into what is called bytecode for the Java virtual machine. "Intermediate code", the Java virtual machine is then responsible for translating the bytecode into the machine code of the platform where the virtual machine is located, and letting the current platform run the machine code.

The operating principle of the Java language:

The bytecode file (that is, the .class file) compiled on a computer can be copied directly to any computer with the Java operating environment installed. run. The bytecode file is then interpreted and executed by the virtual machine. That is, the Java virtual machine translates the bytecode into the machine code of the local computer, and then hands the machine code to the local operating system for running.

Since the bytecode file compiled on a computer can be copied to any computer with a Java runtime environment installed and used directly, the bytecode is actually a kind of "intermediate code", which is Why "Java language can be compiled once and run everywhere, that is why Java is cross-platform." So Java is said to be a "semi-compiled, semi-interpreted language", that is, source program -> compilation -> bytes Code——>Bytecode interpreter——>Machine language corresponding to the platform. ”

The above is the detailed content of Why is Java language platform independent?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template