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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.