Concepts / Running a Java Program

Running a Java Program

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

  • Programming

One Program, Many Computers

Imagine preparing one Java program for several different types of computers. A natural first guess is that the program must be translated into the physical machine language of each computer. Java takes a different route. It does not make every computer understand the same physical machine language directly. Instead, the program is compiled into Java bytecode, a machine language designed for a computer that is virtual rather than physically present. That virtual computer is the Java Virtual Machine, usually called the JVM.

This article follows one simple program from its source file to the message it displays. The goal at this stage is not to understand every detail at once. Instead, you will learn to inspect the complete structure, identify the command that displays the message, and follow the program from its starting point to its output.

From Source File to Bytecode

A Java program starts as source code written by a programmer. That source code is then compiled. The result of compilation is Java bytecode, which is the JVM's machine language. Bytecode is therefore the form of the program that the virtual computer is designed to run.

Java source programCompilationproduces bytecodeJava bytecodeBytecode interpreterimplements the JVMOutput on standardoutputcompiledyieldsrun bydisplays
What happens to the Java source file step by step before the program's output appears, and which step turns it into bytecode? The compilation step turns the source into Java bytecode; the bytecode is then run by the bytecode interpreter that implements the JVM.

Read the diagram from left to right. The compilation step is the only point where source code is converted into bytecode. Once the bytecode exists, the remaining steps are about running it. The output appears only after the interpreter has carried out the instructions in the bytecode.

The JVM as a Virtual Computer

Definition

The Java Virtual Machine (JVM) is a virtual computer, not a physical machine. It is the computer that Java bytecode is written for. Its machine language is Java bytecode.

Java bytecodethe compiled programJava Virtual Machinea virtual computerBytecode interpreterimplements the JVM for onetype of computerPhysical computerthe real hardwarewritten forimplemented byruns on
Where does the JVM sit between the compiled bytecode and the real hardware, and what layers does a single program pass through? The bytecode is written for the virtual JVM, and a bytecode interpreter implements that JVM on one particular physical computer.

The layers in the diagram have different jobs. The bytecode is the compiled program. The JVM is the virtual computer that the bytecode is designed for. The bytecode interpreter is the piece that implements the JVM on a particular type of computer. The physical computer is the real hardware that does the work. A single program passes through all of these layers, from the bytecode at the top down to the hardware at the bottom.

Why the Same Bytecode Runs Everywhere

Java achieves platform independence by pairing the same compiled bytecode with suitable interpreters on different computers. The bytecode does not change from one machine to another. What changes is the bytecode interpreter installed on each computer. Each interpreter implements the JVM for its own particular type of computer, so the same bytecode can be carried out on each of them.

One compiledbytecode fileInterpreter forcomputer type AProgram output oncomputer AInterpreter forcomputer type BProgram output oncomputer Bcarried out bycarried out byproducesproduces
How does one bytecode file reach different types of computers, and what differs on each machine while the bytecode stays the same? Each computer has its own bytecode interpreter implementing the JVM, and the bytecode file itself is unchanged.
Key point

The bytecode is the constant part and the interpreter is the variable part. A compiled Java program stays the same, while each type of computer supplies its own interpreter to run it.

  • Common mistake

    Believing that each computer needs its own compiled version of the program

    Why it fails

    The same compiled bytecode is paired with suitable interpreters on different computers. The bytecode is not rewritten for each machine.

    Do this instead

    Keep one bytecode file and recognize that the machine-specific part is the interpreter that implements the JVM.

  • Common mistake

    Thinking the JVM is a physical machine you can buy or point to

    Why it fails

    The JVM is a virtual computer. It exists as a specification implemented by software, not as hardware.

    Do this instead

    Describe the JVM as a virtual computer that runs bytecode, and describe the physical computer separately.

Anatomy of a Basic Java Program

A first Java program can display the message Hello World! on standard output. The complete program below is a small example written for this article. Inspect its structure first, then identify the command that displays the message.

Code walkthrough
// This comment is ignored by the compiler.
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}
  1. 1
    Line 1 is a comment. Comments document Java code and are ignored by the compiler.
  2. 2
    Line 2 declares the public class HelloWorld. A Java program is defined within a public class.
  3. 3
    Line 3 declares the main method. The main method provides the program's entry point, which is where execution begins.
  4. 4
    Line 4 is the println statement. It displays Hello World! on standard output.
  5. 5
    Lines 5 and 6 close the method and the class.
  • The comment line is documentation only
  • The public class encloses the whole program
  • The main method is the entry point
  • The println statement displays the message
Output
Hello World!

Tracing the Program to Its Output

When a program is run, the JVM begins at the main method. From there the statements inside main are carried out in order. The program is traced by following that path: from the public class, to the main method, and then to the println statement. Tracing this path separates the program's structure from the single command that is responsible for the visible output.

What do you think happens?

In the HelloWorld program, which line actually sends the message to the console?

  • The comment on line 1

  • The public class line

  • The main method header

  • The System.out.println line

Reveal answer
Answer:

The System.out.println line

The class and the method are the structure that holds and starts the program. The println statement is the command that displays Hello World! on standard output, so it is the line that produces the output.

JVMstarts the programmain methodentry pointSystem.out.printlnsends the messageStandard outputHello World!1. calls main2. runs statement3. displays message
Starting from the line that runs the program, which line of code actually sends each message to the console? The JVM enters the main method, and the println statement is the step that sends the message to standard output.

The trace shows why the output appears where it does. The main method is the starting point, and the println statement is the single line that carries the message out to standard output. If the message were wrong or missing, you would begin your inspection at the println statement, after confirming that the path from the class to main is correct.

Mistakes When Tracing a Program

  • Common mistake

    Looking for the output in the class declaration or the comment

    What it looks like

    Reading the public class line and expecting Hello World! to be printed there

    Why it fails

    The class and method provide structure and the starting point. The output comes from the println statement.

    Do this instead

    Locate the System.out.println call, which is the command that displays the message.

  • Common mistake

    Treating a comment as a statement that runs

    What it looks like

    Expecting the text after two slashes to appear on standard output

    Why it fails

    Comments are ignored by the compiler, so they do not run and do not produce output.

    Do this instead

    Skip comments when tracing and focus on the statements inside main.

  • Common mistake

    Assuming the statements inside main run in a random order instead of top to bottom

Best practice

When a trace does not produce the expected result, inspect the path from the class to the main method and then to the println statement. This separates the program's structure from the command responsible for the visible output.

Practice: Trace the Output

EASY

Using the HelloWorld program from this article, identify the public class, the main method, and the line that produces the output. Then explain in one or two sentences which line you would change if you wanted the program to display a different message, and why the other lines would stay the same.

Hints
  • Start at the public class and find the main method inside it.
  • Look for the single println statement inside main.
  • Comments are ignored by the compiler, so they do not affect the message.
MEDIUM

Explain, in your own words, why the same compiled bytecode can run on two different types of computers, naming the part that differs between them.

Hints
  • Think about what the bytecode is written for.
  • Think about which piece implements the JVM on each computer.

Key Takeaways

  • The Java Virtual Machine is a virtual computer, not a physical machine, and its machine language is Java bytecode.
  • A Java program is compiled into bytecode, and a bytecode interpreter implements the JVM for a particular type of computer.
  • The same compiled bytecode runs on different computers because each computer supplies its own suitable interpreter.
  • A basic Java program is defined within a public class, starts at the main method, and displays its message with System.out.println().
  • Comments document code and are ignored by the compiler, so tracing a program means following the path from the class to main to the println statement.