Home>Article>Java> Why does a Java program "compile once and run everywhere"?

Why does a Java program "compile once and run everywhere"?

(*-*)浩
(*-*)浩 forward
2019-09-16 15:29:18 4258browse

Compared with other languages, Java programs can be "compiled once and run anywhere", which shows that it is very cross-platform. But what exactly is this kind of cross-platform in Java? Before that, let’s first understand what’s wrong with not being able to be cross-platform.

Why does a Java program

High-level languages such as C or C are close to human-readable languages and are closer to English grammar. But the computer only recognizes machine instructions composed of sequences of 0 and 1. Therefore, C or C language must be translated into machine instructions composed of a sequence of 0 and 1 that the computer can recognize. The person responsible for this kind of translation is the "compiler".

The problem is that the sequences of 0 and 1 recognized on each platform are different. A certain instruction may be 0101 on Windows, but it may be 1010 under Linux. Therefore, an incompetent compiler must be used to compile executable machine code for different platforms. Programs compiled on Windows cannot be directly obtained on Linux. Wait for other platforms to execute.

That is to say, programs written in C or C language cannot achieve the cross-platform purpose of "compile once and run everywhere".

Java is also a high-level language. In order for the computer to execute the Java program you write, it must also be compiled by a compiler. However, the Java compiler does not directly compile the Java source code into a sequence of 0 and 1 that depends on the computer platform, but compiles it into bytecode.

The extension of Java source code is .java. After being compiled by the compiler, bytecode with the extension .class is generated. If you want to execute a bytecode file, the target platform must have a JVM (java virtual machine) installed. The JVM will translate the bytecode into platform-dependent computer instructions, that is, a sequence of 0, 1.

But you must remember: For platforms that cannot, you must install a JVM specific to that platform. This is like if you speak Chinese (*.java), the Java compiler will help you translate it into English (*.class). After this English file is sent to each country, it will be translated into the local language by local people (JVM) who can understand English. Language (machine instructions).

So one of the responsibilities of the JVM is to be a local translator, translating the bytecode files into a sequence of 0 and 1 that the platform can understand at that time. With the JVM, your Java program can achieve "compile once" "Run Anywhere" for cross-platform purposes. So here we go. We now know that the fundamental reason why Java programs are cross-platform is the reason why the Java virtual machine JVM exists.

The important understanding of JVM is:

For Java programs, we only know one operating system, and this system is JVM, bytecode The file (a document with a .class extension) is the executable file of the JVM.

Ideally, a Java program does not care which platform it is actually executed on, as long as it knows how to execute it on the JVM. As for how the JVM actually communicates with the underlying platform, that is the JVM's own business. Since the JVM is actually equivalent to the operating system of the Java program, the JVM is responsible for various resource management of the Java program.

We need to remember two points:

1. The JVM is the operating system of the Java program, and the executable file of the JVM is the .class file.

2. The Java virtual machine shields the differences between operating systems, but different systems use different virtual machines.

The above is the detailed content of Why does a Java program "compile once and run everywhere"?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete