Concepts / Java Compilation and Java Bytecode

Java Compilation and Java Bytecode

The JVM is a virtual computer, not a physical machine.

  • Programming

One Program, Many Computers

Imagine you have written one Java program and want it to run on several different types of computers. A natural question is whether you would need to prepare a separate version of the program for each machine. Java is designed so that the answer is no, and understanding why is the goal of this article.

What do you think happens?

When one Java program is prepared for several different types of computers, does the compiled program have to be different for each computer?

  • Yes, each computer type needs its own compiled version

  • No, the same compiled program is used on every computer

Reveal answer
Answer:

No, the same compiled program is used on every computer.

Java does not make every computer understand the same physical machine language directly. Instead, the program is compiled into Java bytecode, and that one compiled form is paired with a suitable interpreter on each type of computer.

From Java Source to Java Bytecode

A Java program starts as source code written by a programmer. That source is then compiled. The output of compilation is Java bytecode, which is the machine language of the Java Virtual Machine. Bytecode is therefore not the language of a physical chip; it is the language of a computer that exists in software.

Java source programwritten by the programmerJava compilercompiles the sourceJava bytecodeJVM machine languagecompiled byproduces
What steps does a Java program go through before it is ready to run, and what does each step produce?
  1. 1
    Step one: the programmer writes the Java program as source code.
  2. 2
    Step two: the Java compiler compiles that source program.
  3. 3
    Step three: the compiled output is Java bytecode, the machine language the JVM runs.

What the Java Virtual Machine Is

Definition

The Java Virtual Machine, or JVM, is a virtual computer that runs Java bytecode. It is virtual rather than physically present: it is a computer described and implemented in software, designed to execute the bytecode that Java programs are compiled into.

The phrase virtual computer is the key to the whole design. A physical computer has a processor that understands its own native machine instructions. The JVM instead defines its own machine language, bytecode, and the bytecode is written for that virtual machine rather than for any one physical processor.

Layers Between Bytecode and Hardware

A compiled Java program does not reach the physical computer directly. It sits at the top of a stack of layers. The bytecode is the program's compiled form. The JVM is the virtual computer that runs that bytecode. A bytecode interpreter is the piece that implements the JVM for one particular type of computer, and the physical computer is the hardware that finally carries out the work.

Java bytecodecompiled Java programJava Virtual Machinevirtual computerBytecode interpreterimplements the JVM for onecomputer typeParticular computerphysical hardwarerun byimplemented byruns on
Which layers sit between the compiled bytecode and the physical computer, and what does each layer contain or depend on?

How an Interpreter Connects a Program to Hardware

A Java bytecode interpreter implements the JVM for a particular type of computer. This is the bridge. The bytecode stays the same, but the interpreter on a given computer knows how that computer works, so it connects the bytecode instructions to the machine instructions that the particular CPU can execute.

Bytecode instructionfrom the compiled programBytecode interpreterspecific to one computertypeMachine instructionsfor that computer's CPUCPUexecutes the instructionsread byturned intoexecuted by
How does a bytecode instruction travel through the interpreter and become machine instructions that a particular CPU executes?

Why the Same Program Runs Everywhere

Java achieves platform independence by pairing the same compiled bytecode with suitable interpreters on different computers. The table below separates what stays identical across machines from what changes.

Part of the systemSame on every computer?Why
Compiled Java bytecodeYesIt is written for the virtual machine, not for one physical CPU
Java Virtual MachineYes, as a specificationIt is the virtual computer the bytecode is designed for
Bytecode interpreterNoIt implements the JVM for one particular type of computer
Physical computer and CPUNoEach type of computer has its own hardware

What stays identical and what differs when one compiled Java program runs on different computers

Worked example

Preparing One Program for Three Computers

A developer compiles a Java program once and then wants it to run on three different types of computers. Which parts are prepared once, and which parts are prepared separately for each computer?

  1. 1
    Compile the source once

    The Java source is compiled into Java bytecode. This happens once, producing one compiled program.

  2. 2
    Identify what is shared

    The same bytecode is handed to all three computers. Nothing in the bytecode is rewritten for each machine.

  3. 3
    Pair each computer with its interpreter

    Each of the three computers needs a bytecode interpreter suitable for its own type. The interpreter implements the JVM on that computer.

  4. 4
    Run the same program

    Each interpreter connects the shared bytecode to the machine instructions of its own CPU, so the same program runs on all three.

Result

One compiled program is shared across all three computers. The only piece that is specific to each computer is the bytecode interpreter that implements the JVM there.

Mistakes About Bytecode and the JVM

  • Common mistake

    Treating Java bytecode as the native machine language of a physical CPU

    Why it fails

    Bytecode is the machine language of the Java Virtual Machine, a virtual computer. A physical processor does not run bytecode directly; an interpreter implements the JVM on that processor's type of computer.

    Do this instead

    Describe bytecode as the JVM's machine language, produced by compiling Java programs.

  • Common mistake

    Thinking the JVM is a physical chip or piece of hardware

    Why it fails

    The JVM is a virtual computer. It is described and implemented in software, not a physical machine.

    Do this instead

    Call the JVM a virtual computer that runs Java bytecode.

  • Common mistake

    Assuming every computer type needs its own compiled version of the program

    Why it fails

    The same compiled bytecode is paired with suitable interpreters on different computers, so the compiled program itself is shared.

    Do this instead

    Keep the bytecode fixed and change only the interpreter that implements the JVM on each computer.

  • Common mistake

    Confusing the compiler with the interpreter, when the compiler produces bytecode and the interpreter connects bytecode to a particular computer

Trace the Path Yourself

EASY

A Java program has been compiled once into bytecode. It is then run on two different types of computers. For each of the following, state whether it stays the same or changes between the two computers: the source program, the bytecode, the JVM as a specification, the bytecode interpreter, and the physical CPU. Then explain in one or two sentences why the program still behaves the same way on both.

Hints
  • Start with the compiled bytecode and ask whether it was rewritten for each computer.
  • Think about which piece is described as implementing the JVM for one particular type of computer.
  • The bytecode interpreter is the component that differs between computer types.

Key Takeaways

  • The Java Virtual Machine, or JVM, is a virtual computer that runs Java bytecode rather than a physical machine.
  • Java source programs are compiled into Java bytecode, which is the machine language of the JVM.
  • A Java bytecode interpreter implements the JVM for a particular type of computer and connects the bytecode to that computer's machine instructions.
  • The same compiled bytecode is paired with suitable interpreters on different computers, which is how Java achieves platform independence.