How Java Runs Your Code

Two steps: compile, then run

A computer can't understand Java directly. So Java works in two steps:

  1. Compile – a tool called the compiler (javac) checks your code for mistakes and turns it into bytecode, a simpler form of the instructions.
  2. Run – the JVM (Java Virtual Machine) reads the bytecode and actually does what it says.

Think of it like this: The compiler is like a teacher who checks your homework for spelling mistakes before you hand it in. The JVM is like a translator who then reads your homework out loud in the computer's own language.

Write once, run anywhere

Windows, Mac, Linux and Android all have their own JVM. Because the bytecode is the same everywhere, the same Java program runs on all of them. This is Java's famous motto: “Write once, run anywhere.”

The words you will hear

WordMeaning
Source codeThe Java text you write, saved in a file ending in .java
Compiler (javac)Checks your code and turns it into bytecode
BytecodeThe compiled program, saved in a .class file
JVMThe program that runs the bytecode
JDKJava Development Kit: the compiler + JVM + tools you install to write Java

Two kinds of errors

  • Compile errors – found by the compiler before the program runs, like a missing ;.
  • Runtime errors – happen while the program is running, like dividing a number by zero.

Tip: In this course you don't need to install anything: the challenge pages have a compiler and a JVM that run inside your web browser. When you want to write Java on your own computer, install a JDK (for example from adoptium.net) and an editor such as IntelliJ IDEA or VS Code.

HOME LABS COMMUNITY DASHBOARD