The basic idea that made Java revolutionary when first introduced was as simple as it was captivating: Java would make it possible to write a program without being tied to a specific hardware or operating system. Such platform independence is often summed up by the phrase “write once, run anywhere”. The associated portability allows the program to be moved between platforms. What a great trick! So how does it work?
As with other programming languages, a Java program begins with source code that can be read by a human. In order to execute the instructions of the source text on a computer, corresponding instructions are generated in the format of the specific processor. With Java, there is another intermediate step: The source text is first translated into an intermediate format, the so-called bytecode, as is the case with the Python language. The bytecode is then executed in the “Java virtual machine” (JVM). In order to run a Java program on a device, a JVM must be installed on it.
The bytecode is traditionally interpreted for execution in the JVM. The bytecode instructions are translated piece by piece into machine code instructions and executed. The process of “just-in-time compilation” (JIT) is more effective. With that process, the bytecode is also converted into machine code, but further optimizations also come into play. In sum, running a Java program involves the following steps:
- Compiling Java source code to bytecode with the Java compiler command ‘javac’: