Home  >  Article  >  Java  >  Why is java cross-platform?

Why is java cross-platform?

青灯夜游
青灯夜游Original
2019-05-22 17:51:1422218browse

Cross-platform reasons: Java Virtual Machine (JVM) can be cross-platform; JAVA programs are not run directly on the computer (CPU), but on the Java Virtual Machine (JVM). Each system platform They all have their own JVM virtual machine, so the JAVA language can be cross-platform. Java first compiles Java files into binary bytecode class files, and then JVM interprets and executes the class files; because Java runs on JVM, its code can run on JVMs on different platforms without modification.

Why is java cross-platform?

The operating environment of this tutorial: windows7 system, java8 version, DELL G3 computer.

Why can the JAVA language be cross-platform?

The reason why Java can be cross-platform is because the Java Virtual Machine (JVM) can be cross-platform.

Because the code compiled by the Java program is not code that can be directly run by the hardware system, but an "intermediate code" - bytecode. Then different Java virtual machines (JVM) are installed on different hardware platforms, and the JVM "translates" the bytecode into code that can be executed by the corresponding hardware platform. Therefore, for Java programmers, there is no need to consider what the hardware platform is. So Java can be cross-platform.

Why is java cross-platform?

Because it has a virtual machine (JVM), the JAVA program is not run directly on the computer, but on the java virtual machine (JVM for short). Each system Each platform has its own virtual machine (JVM), so the JAVA language can be cross-platform.

  • Java code does not run directly on the CPU, but on the Java Virtual Machine (JVM).

  • Java first compiles the java file into a class file of binary bytecode, and then the jvm interprets and executes the class file.

  • Because java runs on jvm, its code can run on jvm on different platforms without modification.

Expand knowledge:

The professional explanation of JVM is as follows

Java Virtual Machine (JVM for short) is a specification for computing devices. It is a fictitious computer that is implemented by simulating various computer functions on an actual computer. The Java virtual machine consists of a set of bytecode instructions, a set of registers, a stack, a garbage collection heap, and a storage method field. The JVM shields the information related to the specific operating system platform, so that the Java program only needs to generate the target code (bytecode) that runs on the Java virtual machine, and it can run on multiple platforms without modification. When the JVM executes bytecode, it actually ultimately interprets the bytecode into machine instructions for execution on the specific platform.

It’s normal to not understand the above explanation. I didn’t want to post a professional explanation at first, but after thinking about it I decided not to. After all, I still want everyone to have a detailed understanding of the JVM. If you don’t understand the professional explanation, I will tell you in plain English:

JVM, which is the Java virtual machine, is a platform, included under the JRE. When you need to execute a Java program, the JVM compiles and executes it for you. The Java source code we write will generate a .class file after compilation, called a bytecode file. The Java virtual machine is responsible for translating bytecode files into machine code for a specific platform and then running it.

And the JVM is divided into many system versions. No matter which platform you write the Java program on, whether it is Windows, Linux, or macOS, you only need to find the JVM and it will be compiled successfully. and execution. In other words, as long as the corresponding JVM is installed on different platforms, the bytecode files can be run and the Java programs we wrote can be run. So this is the reason why Java has good cross-platform and platform-independent features.

During this process, the Java program we wrote did not make any changes. It can run on different platforms only through the "middle layer" of JVM, truly realizing "compile once and run anywhere" the goal of.

JVM is a "bridge", a "middleware", and the key to achieving cross-platform. Java code is first compiled into bytecode files, and then the JVM translates the bytecode files into machine language , thereby achieving the purpose of running Java programs.

Note: The result of compilation is not to generate machine code, but to generate bytecode. The bytecode cannot be run directly and must be translated into machine code by the JVM before it can be run. The bytecode generated by compilation under different platforms is the same, but the machine code translated by the JVM is different.

So, running a Java program must have the support of the JVM, because the compiled result is not machine code and must be translated again by the JVM before it can be executed. Even if you package your Java program into an executable file (such as .exe), you still need JVM support.

Note: Cross-platform is a Java program, not a JVM. JVM is developed using C/C. It is compiled machine code and cannot be cross-platform. Different versions of JVM need to be installed on different platforms.

About JVM execution efficiency

In the first few years after Java was launched, people had different opinions. Interpreting bytecode must be much slower than running machine code at full speed. Is it worth sacrificing performance in exchange for cross-platform advantages?

However, the JVM has an option to translate the most frequently used bytecode into machine code and save it, a process called just-in-time compilation. This approach is so effective that Microsoft's .NET platform also uses virtual machines.

Now's just-in-time compilers are so good that they have even become competitors of traditional compilers. In some cases, they even surpass traditional compilers because the JVM can monitor runtime information. For example, a just-in-time compiler can monitor and optimize frequently used code and can eliminate function calls (i.e., "inline").

However, Java has some additional overhead that C/C does not have, and critical applications are slower. For example, Java uses a platform-independent drawing method, so the GUI program (client program) executes slowly; the virtual machine also takes time to start.

Recommended related video tutorials: Java video tutorial

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

Statement:
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