How Java Works
Java is one of the most widely used programming languages in the world, running on everything from Android phones to bank servers to the Mars rovers. That reach isn’t an accident. It comes down to one design decision made in the early 1990s, and understanding it will make the rest of Java click into place.
The problem Java was built to solve
In the early 1990s, if you wrote a program in C or C++, you had to compile it separately for every type of computer and operating system you wanted it to run on. Code written for Windows wouldn’t run on a Mac. Code written for a Sun workstation wouldn’t run on an IBM machine. This was genuinely painful, especially as the internet was starting to connect all these different kinds of computers together.
Java’s designers at Sun Microsystems had a simple goal: write the code once, run it anywhere. They called it WORA, short for Write Once, Run Anywhere. The way they achieved it is worth understanding, because it shapes everything about how Java works.
What happens when you write Java
When you write Java code, you’re writing in a language that humans can read. But computers don’t actually execute human-readable code; they execute machine instructions, which look nothing like what you write. Something has to translate your code into something the computer understands.
Java does this translation in two steps, not one. That two-step process is the key to how it achieves “run anywhere.”
Step 1: Compilation
You run the Java compiler (a program called javac) on your code. Java code is written in files that end in .java, which is just the file extension Java uses. The compiler reads that file and translates it into something called bytecode, stored in a new file ending in .class.
Bytecode is not machine code. It’s not instructions for any specific processor. It’s instructions for an imaginary machine, a machine that doesn’t actually exist in hardware.
Step 2: Running on the JVM
That imaginary machine does exist in software. It’s called the Java Virtual Machine, or JVM.
When you run a Java program, the JVM reads the bytecode and executes it. The JVM is what actually talks to the real computer underneath. And here’s the trick: there’s a JVM for Windows, a JVM for macOS, a JVM for Linux, and JVMs for many other platforms. Each one is different, tailored to its operating system. But they all understand the same bytecode.
So your .class file (your compiled bytecode) runs on all of them without any changes. You compile once, and the JVM on each platform handles the rest.
The two kinds of problems you’ll hit
Since there are two steps, there are two places things can go wrong.
Compile-time errors are caught by the compiler before your program ever runs. You made a typo, used a variable that doesn’t exist, or wrote something that isn’t valid Java. The compiler refuses to produce bytecode, and your program never runs at all. These are actually the friendlier kind of error, the compiler tells you exactly what’s wrong and where.
Runtime errors happen while the program is running. The compiler was satisfied, bytecode was produced, the JVM started executing, and then something went wrong. Maybe you tried to divide by zero. Maybe you accessed an array element that doesn’t exist. The program crashes and prints an error message describing what happened and where.
You’ll see both kinds constantly. For now, just know they’re different and they come from different stages of the process.
What you need installed
You don’t need to install anything to run code in this course. The platform runs Java for you in the browser. Just hit “Run” on any code block and it works.
If you want to work on your own machine as well, here’s what you need:
-
A JDK (Java Development Kit): This includes the compiler (
javac) and the JVM, plus a bunch of tools and standard libraries. Java has several distributions, all based on the same open-source core. For learning, any of them work fine. A safe, free choice is Adoptium’s Eclipse Temurin at adoptium.net, which is vendor-neutral and widely used in production. Avoid the official Oracle JDK unless you know you need it; it requires a paid license for commercial use. Whichever you choose, pick the latest LTS (long-term support) version, which gets updates and bug fixes for years. -
A code editor or IDE: An IDE (Integrated Development Environment) is a program that helps you write code. It handles compilation automatically, highlights errors as you type, and has tools for running and debugging. IntelliJ IDEA (Community Edition is free) is the most widely used Java IDE in industry. VS Code with the Java Extension Pack is a lighter alternative if you prefer something simpler to start.
Once you have a JDK installed, you can verify it worked by opening a terminal and typing:
java -versionYou should see something like openjdk version "25.0.3" or similar. If you get “command not found,” the JDK isn’t installed correctly or your PATH isn’t set up; search for setup instructions specific to your operating system.
A note on Java versions
Java releases a new version every six months, but most of the industry runs on LTS releases: Java 11, Java 17, Java 21, and Java 25. If you’re starting fresh today, install Java 25.