So how does a cross-platform implement it? This will refer to the Java Virtual machine (Java VM, or JVM).
The JVM is also a software that has different versions of different platforms. We write the Java source code, compiled will generate a. class file, called a bytecode file. The Java Virtual machine is responsible for translating bytecode files into machine code under a specific platform and then running. That is, as long as the corresponding JVM is installed on different platforms, you can run the bytecode file and run the Java program we wrote.
In this process, the Java program that we write does not make any changes, just through the JVM, the "middle layer" can be run on different platforms, and really achieve the "compile once, run everywhere" purpose.
JVM is a "bridge", is a "middleware", is the key to achieve cross-platform, Java code is first compiled into bytecode files, and then by the JVM to translate bytecode files into machine language, so as to achieve the purpose of running Java programs.
Note: The result of compiling is not to generate machine code, but to generate bytecode, bytecode can not run directly, must be translated into machine code by the JVM to run. The bytecode generated under different platforms is the same, but the machine code translated by the JVM is not the same.
Therefore, running the Java program must have the support of the JVM, because the result of the compilation is not machine code, it must be re-translated by the JVM to execute. Even if you package a Java program as an executable file (for example,. exe), you still need the JVM's support.
Note: The cross-platform is a Java program, not a JVM. The JVM is developed in C/s + +, is compiled machine code, not cross-platform, different platforms need to install different versions of the JVM.
How does Java implement cross-platform?