Platform Independence in Java
The JVM is a virtual computer, not a physical machine.
One Program, Many Computers
Suppose you have written one Java program and want it to run on several different types of computers. Each of those computers may have different hardware and a different operating system. A natural question follows: how can one program be ready for all of them? This article answers that question by following a Java program from the moment its source is written to the moment it runs on a real machine.
The short answer is that Java does not ask every computer to understand the same physical machine language directly. Instead, the program is translated into a different kind of machine language, one designed for a computer that is virtual rather than physically present. Keep that idea in mind as we trace the steps.
Tracing a Java Program from Source to Execution
A Java program starts as Java source code, which is the text that a programmer writes. That source is compiled into Java bytecode. Bytecode is the machine language of the Java Virtual Machine, so it is the form the program takes once it is ready to be run in Java's own environment. The bytecode is then handled by a Java bytecode interpreter, which carries out the program on a particular type of computer.
- 1Write the Java source program as text.
- 2Compile the source program, which produces Java bytecode.
- 3Hand the bytecode to a Java bytecode interpreter that implements the JVM for the computer in use.
- 4The interpreter carries out the bytecode on that particular type of computer.
The Virtual Computer Behind Bytecode
The Java Virtual Machine, or JVM, is a virtual computer that runs Java bytecode. Its machine language is Java bytecode, which is produced by compiling Java programs.
The word virtual is the key to understanding the JVM. A physical computer is a real piece of hardware sitting on a desk or in a rack. The JVM is not that kind of machine. It is a computer that is defined in software and imitated on top of whatever real computer is actually present. Because the JVM is a defined machine with its own machine language, a compiled Java program can be written against that machine rather than against the specific processor of any one computer.
How the Interpreter Bridges to Real Hardware
Bytecode cannot do its work alone, because the physical computer does not understand the JVM's machine language directly. A Java bytecode interpreter fills that gap. The interpreter implements the JVM for a particular type of computer, so it knows how the virtual machine's instructions correspond to the real hardware and operating system in front of it.
Each interpreter is tailored to one type of computer. The bytecode itself stays the same. What changes from one machine to another is the interpreter that is paired with it. This pairing is how Java achieves platform independence: the same compiled bytecode is combined with a suitable interpreter on each different computer.
The interpreter's work is repetitive. It handles the bytecode one instruction at a time, and the loop below shows the pattern that repeats until the program is finished.
Same Bytecode on Windows, Linux, and macOS
Now we can answer the opening question. The same compiled Java program can run on different types of computers because the program is compiled into bytecode for the JVM, and each type of computer receives its own suitable interpreter. The bytecode is shared; the interpreter is specific to the machine.
Preparing one program for three lab computers
A school has three computer types in its lab: Windows, Linux, and macOS. A teacher compiles one Java program once and wants every lab computer to run it. How does the single compiled program reach all three machines?
- 1Compile once
The teacher compiles the Java source program, which produces Java bytecode. Only one compiled version is created.
- 2Give each computer type an interpreter
Each type of computer has its own Java bytecode interpreter that implements the JVM for that particular kind of machine.
- 3Pair the same bytecode with each interpreter
The identical bytecode is handed to the Windows interpreter on Windows machines, the Linux interpreter on Linux machines, and the macOS interpreter on macOS machines.
- 4Run on each machine
Each interpreter carries out the bytecode on its own computer, so the same program runs on all three types without being rewritten.
One compiled bytecode program runs on Windows, Linux, and macOS because the bytecode is shared and only the interpreter changes from one computer type to another.
Mistakes Beginners Make with Bytecode and the JVM
Common mistake Thinking Java bytecode is the machine code of your processor
What it looks likeAssuming a compiled Java program is already in the exact instructions an Intel or Apple chip runs directly
Why it failsBytecode is the machine language of the JVM, a virtual computer, and the physical processor does not understand it directly.
Do this insteadTreat bytecode as the JVM's instructions, which an interpreter for your computer carries out.
Common mistake Thinking the JVM is a physical machine that must be bought or plugged in
Why it failsThe JVM is a virtual computer, not a physical machine.
Do this insteadThink of the JVM as a defined computer that is implemented in software on a real machine.
Common mistake Believing each computer needs a different compiled version of the program
What it looks likeCreating a separate compiled Java program for Windows and another for Linux
Why it failsThe same compiled bytecode can run on different types of computers, provided each has a suitable interpreter.
Do this insteadCompile once and rely on a suitable interpreter for each computer type.
Common mistake Confusing the interpreter with the source code, when the interpreter is the piece that runs the bytecode on a particular computer
Checking Your Understanding of the Pipeline
What do you think happens?
Suppose a Java program has been compiled to bytecode and a suitable interpreter exists for a Linux computer. Can that same bytecode run on the Linux computer?
Reveal answer
Yes, because the bytecode is shared and the interpreter is matched to Linux
Java achieves platform independence by pairing the same compiled bytecode with suitable interpreters on different computers. The bytecode does not need to be rebuilt for Linux.
Without looking back, explain in your own words the path a Java program takes from source code to running on a computer. Name the step that produces bytecode, the element that implements the JVM for a particular computer, and the reason one bytecode file can serve several computer types.
Hints
- Start with the source program and follow the arrows in the pipeline diagram.
- Ask which step is responsible for producing bytecode and which step is tied to one type of computer.
Key Takeaways
- The JVM is a virtual computer, not a physical machine, and Java bytecode is its machine language.
- A Java source program is compiled into Java bytecode, which is then handled by a Java bytecode interpreter.
- A Java bytecode interpreter implements the JVM for a particular type of computer, connecting the bytecode to that computer's hardware and operating system.
- Java achieves platform independence by pairing the same compiled bytecode with suitable interpreters on different computers.