Sub topic 1
How Java Runs: Bytecode and the JVM
Java is different from many programming languages because of how it runs your code. Understanding this is the key to understanding everything else in this tutorial: why Java programs look the way they do, why Java works on Windows, Mac, and Linux alike, and why you always type those two commands, javac and java.
We assume no programming background. If you can open a terminal and type commands into it, you have everything you need.
The problem Java solves: many computers, one program
A computer's processor (the chip that actually does the work) only understands its own machine code — a list of very low-level instructions written in 1s and 0s. The trouble is that machine code differs from one kind of computer to the next. An Intel laptop, a Mac with an Apple chip, and a Linux server all speak different "dialects" of machine code.
With a language like C, you compile (translate) your program once for one specific kind of computer. If you want it to run on other kinds of computers, you have to compile it again for each one. That's a lot of extra work.
Java takes a different route. You compile your program once, to a halfway language that no real processor understands — but a program called the JVM (Java Virtual Machine, a piece of software that pretends to be a processor) does. The JVM then translates that halfway language into real machine code for whatever computer it is running on.
The halfway language is called bytecode. It is a compact set of instructions, stored in a file ending in .class.
Here is the full journey of a Java program:
The payoff: you write Main.java once, compile it once to Main.class, and that same .class file runs unchanged on any computer that has a JVM. That property is called platform independence, and it is the reason Java's old slogan was "write once, run anywhere."
What the JVM actually is
The words "virtual machine" can sound mysterious. In practice, the JVM is just a program — like a web browser or a music player — that you install on your computer. Its job is to read bytecode and carry out the instructions.
Think of the JVM as a translator standing between your program and the real hardware:
- Your program speaks bytecode.
- The real computer speaks machine code.
- The JVM listens to one and speaks the other.
There is a separate JVM build for Windows, macOS, and Linux, but they all speak the same bytecode. That is what makes portability possible.
The JVM also does more than translate
Two other jobs matter, and we will return to them later in the tutorial:
- Memory management. When your program creates objects (bundles of data, which we meet in the classes and objects chapter), the JVM finds space for them. When they are no longer needed, the JVM cleans them up automatically — a process called garbage collection. You do not manually free memory as you would in C.
- Safety. The JVM checks bytecode before running it. A program running inside the JVM cannot, for example, walk into unrelated memory and corrupt other programs.
Java Development Kit vs. Java Runtime Environment
You will see two bundles mentioned:
| Bundle | Usually called | Contains | Who needs it |
|---|---|---|---|
| Compile and run tools | JDK (Java Development Kit) | The compiler `javac`, the JVM, and libraries | Writers of Java programs |
| Run only | JRE (Java Runtime Environment) | The JVM and libraries | People who only run Java programs |
If you plan to write code — as you will in this tutorial — install a JDK. It includes the JVM too.
Compiling and running by hand
Let's make the whole pipeline concrete. Create a file named Main.java with exactly this content:
public class Main {
public static void main(String[] args) {
System.out.println("Java is running!");
}
}Notice a few things, which later chapters explain fully:
- The file is named after the class: a
classis the container Java code lives in. The file must beMain.javabecause the class isMain. - The
mainmethod is the entry point — the place the JVM starts your program. System.out.println(...)prints a line of text.
Now, in a terminal in the same folder, run:
javac Main.javajavac (pronounced "java-see") is the compiler. If it succeeds, there is no message — but a new file, Main.class, appears. Peek inside it with a text editor and you will see mostly gibberish starting with the characters CAFEBABE. That is bytecode: compact, unreadable to humans, and not tied to any real processor.
(Yes, CAFEBABE is really the magic number at the start of every Java class file. The Java team had a sense of humor.)
Next, run the program:
java MainYou should see:
Java is running!Notice that you pass Main, not Main.class, to the java command. The JVM finds and loads the .class file itself. Since Java 11, you can also skip the explicit compile step for simple single-file programs: java Main.java compiles and runs in one go. The two-step version is still worth learning because larger projects always compile first.
What bytecode looks like
You will never need to write bytecode, but seeing a taste of it makes the model real. The JDK ships with a tool called javap that reads .class files. Run:
javap -c Mainand you will see a disassembly — a human-readable listing — of the bytecode. The essential lines look roughly like this:
public static void main(java.lang.String[]);
Code:
0: getstatic #2 // fetch the "out" print stream
3: ldc #3 // load the text constant "Java is running!"
5: invokevirtual #4 // call println on it
8: return // end of mainEach line is one simple instruction: fetch something, load something, call a method, return. The JVM reads these instructions one by one. That is all "running Java" means.
How the JVM executes bytecode
The JVM does not interpret bytecode naively forever; that would be slow. Instead it uses a technique called JIT compilation — "just in time."
Here is the idea:
- When your program starts, the JVM interprets the bytecode directly: it reads each instruction and does what it says. This is simple but not fast.
- The JVM watches which parts of your code run most often — usually a small fraction, such as the inner loops.
- Once a piece of code is "hot" (run many times), the JVM compiles it on the fly into real machine code for this specific computer.
- From then on, that hot code runs at full native speed.
This design has a pleasing consequence: Java code does not pay the cost of machine-code compilation at startup, yet long-running programs get close to compiled-language speed where it matters. It also reinforces portability: the machine code is produced fresh on each computer, at run time, so it always matches that computer's processor.
Common misunderstandings
- "Java is slow." This reputation dates from the 1990s, before JIT compilation matured. Today, hot code in a long-running Java program typically runs at speeds comparable to C or C++. Startup and short scripts remain Java's weak spot.
- "The JVM and the compiler are the same thing." They are distinct tools:
javactranslates your source code to bytecode; the JVM executes bytecode. You compile once; the JVM can run the result many times, anywhere. - "You must recompile for each operating system." You do not. A
.classfile built on Linux runs unmodified on Windows and macOS, as long as each has a JVM of a compatible version. - "Bytecode is machine code." It is not. Bytecode is an invented instruction set for a machine that exists only as software — the JVM.
- "The JVM is only for Java." Other languages, such as Kotlin and Scala, also compile to bytecode and run on the JVM. The JVM is a platform that several languages share.
Why this matters for the rest of the tutorial
The pipeline you just ran — edit Main.java, run javac, run java — is the loop you will repeat for every chapter ahead. Keep three points in mind as we go:
- Every piece of Java code lives inside a class, and every program starts at
main. The next chapter takes apart that structure line by line. - When the compiler complains, it complains before anything runs: many mistakes are caught at compile time. This is one of Java's strengths, and you will learn to read its messages.
- When your program creates objects, it is the JVM — not you — that manages their memory. That guarantee is explored in the final chapter of this tutorial.
Chapter summary
- Computers run machine code, which differs by processor; Java avoids rewriting programs for each one.
javaccompiles.javasource files into bytecode stored in.classfiles.- The JVM (Java Virtual Machine) executes bytecode on any platform, giving Java platform independence: write once, run anywhere.
- The JVM also manages memory automatically and checks code for safety.
- JIT compilation turns frequently used bytecode into native machine code while the program runs, so long-running Java programs are fast.
- You need the JDK (compiler plus JVM) to write programs; the commands
javacandjavaare the compile and run steps.
Exercise
What is bytecode, and why can the same bytecode run on Windows, macOS, and Linux?
Show answer
Bytecode is a set of instructions for an imaginary processor, produced by the Java compiler and stored in .class files. No real processor understands it directly. Each operating system has its own JVM (Java Virtual Machine) that translates the same bytecode into that machine's real machine code at run time, so one compiled file runs everywhere a JVM exists.
Exercise
You have a file called Program.java. Name the two commands used to compile and then run it, and the file the first command produces.
Show answer
First javac Program.java, which produces bytecode in Program.class. Then java Program (note: no .class extension), which runs it on the JVM.
Exercise
What is JIT compilation, and why does it not break Java's portability?
Show answer
JIT ("just in time") compilation is when the JVM, while your program runs, converts frequently used bytecode into real machine code for the current computer. It does not break portability because the conversion happens at run time on each machine: the .class bytecode file is still identical everywhere, and each machine's JVM produces machine code that fits its own processor.