Sub topic 1
How Java Runs: Bytecode and the JVM
Before you write your first line of Java, it helps to know what happens to that line. Java has a famous slogan: "write once, run anywhere." This chapter explains what that means and why it works.
The problem Java solves
Computers only understand machine code. Machine code is a set of very low-level instructions that are specific to one kind of processor. A program compiled for a Windows PC will not run on a Mac or a phone, because their processors expect different instructions.
Older languages like C compile directly to machine code. That makes them fast, but not portable. Portable means the same program can run on many different kinds of computers without changes.
The Java solution: two steps
Java splits the work into two steps:
- Compile: A tool called the compiler (
javac) reads your human-readable source code (files ending in.java) and translates it into bytecode. Bytecode is an intermediate language. It is not machine code for any specific processor. It is stored in files ending in.class. - Run: A program called the JVM, or Java Virtual Machine, reads the bytecode and translates it into machine code for whatever computer it is running on. The JVM is a virtual machine: a software program that acts like a computer.
The diagram below shows this flow:
You write and compile your code once. Then any device with a JVM can run it. That is "write once, run anywhere."
What the JVM actually does
The JVM is more than a translator. As it runs your program, it also:
- Loads your
.classfiles when they are needed. - Verifies the bytecode to make sure it is safe and well-formed.
- Executes the bytecode, turning it into real instructions for the processor.
- Manages memory for you, including cleaning up objects your program no longer uses. Later chapters cover this in more detail.
Because the JVM does these jobs, you can focus on what your program should do, not how each machine works.
A small taste of bytecode
You do not need to read bytecode in this tutorial, but seeing it once makes the idea concrete. Suppose you have this simple file:
public class Hello {
public static void main(String[] args) {
System.out.println("Hello!");
}
}After compiling with javac Hello.java, you get Hello.class. If you run javap -c Hello (a tool that displays bytecode), you will see instructions like getstatic, ldc, and invokevirtual. These are bytecode instructions. Humans rarely read them; the JVM does.
Why this matters to you
Three practical takeaways:
- Errors come in two kinds. Some problems stop the compiler (compile-time errors, like a missing semicolon). Others appear only when the JVM runs the program (runtime errors). Learning to tell them apart will save you time.
- Your
.javafiles are for people. The.classfiles are for machines. You share either one, but you only edit source code. - Portability is free. The same
.classfile runs everywhere a JVM exists, with no recompiling.
With this model in mind, the next chapter looks at what a Java program is actually made of: classes and the main method.
Exercise
A friend compiles a Java program on a Mac and sends you only the .class file. Can you run it on a Windows computer?
Show answer
Yes. The .class file contains bytecode, which is not tied to any one platform. As long as your Windows computer has a JVM installed, it can run the bytecode.
Exercise
What is the difference between the compiler and the JVM?
Show answer
The compiler (javac) translates your source code into bytecode. The JVM reads that bytecode and executes it as real machine instructions on a specific computer.