Running a Java Program
The JVM is a virtual computer, not a physical machine.
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.
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
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.
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.
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 failsThe same compiled bytecode is paired with suitable interpreters on different computers. The bytecode is not rewritten for each machine.
Do this insteadKeep 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 failsThe JVM is a virtual computer. It exists as a specification implemented by software, not as hardware.
Do this insteadDescribe 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.
// This comment is ignored by the compiler.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}- 1Line 1 is a comment. Comments document Java code and are ignored by the compiler.
- 2Line 2 declares the public class HelloWorld. A Java program is defined within a public class.
- 3Line 3 declares the main method. The main method provides the program's entry point, which is where execution begins.
- 4Line 4 is the println statement. It displays Hello World! on standard output.
- 5Lines 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
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?
Reveal 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.
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 likeReading the public class line and expecting Hello World! to be printed there
Why it failsThe class and method provide structure and the starting point. The output comes from the println statement.
Do this insteadLocate 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 likeExpecting the text after two slashes to appear on standard output
Why it failsComments are ignored by the compiler, so they do not run and do not produce output.
Do this insteadSkip 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
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
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.
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.