Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 201 ยท Getting Started

How Java Works: JDK, JVM and Bytecode

Follow a Java program from source code to bytecode to the JVM. Learn what the JDK, JRE and JIT compiler are, which LTS version to install and how to run Java files.

When you press Run on a Java example, a lot happens in about a second: your text is checked, translated into a compact instruction format called bytecode, loaded into a virtual machine, and then turned into real machine code while it runs. Knowing these steps explains Java's error messages, its "write once, run anywhere" promise, and why Java programs often get faster the longer they run.

This lesson walks through the whole pipeline, then covers the practical side: what the JDK is, which version to install, and the commands you'll use on your own computer.

From source code to running program

A Java program goes through three stages:

  1. Source code. You write human-readable code in a file ending in .java, such as Greeter.java.
  2. Compilation. The Java compiler, javac, reads the source, checks it for errors and produces a .class file containing bytecode: instructions for an imaginary computer, not for any real processor.
  3. Execution. The java launcher starts the Java Virtual Machine (JVM), which loads the .class file, verifies it, and executes the bytecode.
TEXT
Greeter.java  --javac-->  Greeter.class  --java (JVM)-->  running program
 (source)                  (bytecode)                       (machine code)

On the command line, the two classic steps look like this:

Terminal
javac Greeter.java     # compile: creates Greeter.class
java Greeter           # run: starts the JVM with the class Greeter

Notice that java Greeter takes a class name, not a file name. That small detail trips up many beginners.

Seeing real bytecode

Bytecode is normally invisible, but the JDK ships tools to look at it. The program below writes a tiny class to a temporary folder, compiles it with javac from inside Java, and then shows the result with javap, the class-file disassembler. It's more advanced code than you'll write for a while, so just run it and look at the output:

Java Compile a class and look at its bytecode

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

A few things stand out in the output:

  • Every class file starts with the bytes CA FE BA BE, a "magic number" the JVM checks before loading anything. (Yes, it spells "cafe babe", a joke from Java's early days.)
  • The compiler already worked out 2 + 40: the bytecode pushes the constant 42 directly (bipush 42). Compilers fold constant arithmetic like this.
  • Instructions such as istore_1 (store an int in local variable 1) and getstatic (load System.out) are bytecode operations. They are simple, compact and identical on every operating system.
  • The compiler added a constructor, public Greeter(), that you never wrote. Every class gets a default constructor if it doesn't declare one.

You'll never need to write bytecode, but seeing it once makes the JVM much less mysterious. The JVM Architecture and Class Loading lesson goes deeper.

The Java Virtual Machine

The JVM is a program that runs bytecode. There are JVMs for Windows, macOS and Linux on Intel and ARM processors, and each one understands exactly the same bytecode. That is what write once, run anywhere means: you compile once, and the same .class or .jar file runs on any machine that has a JVM. The JVM, not your program, deals with the differences between operating systems.

Besides running instructions, the JVM does a lot of work for you:

  • Verification. Before running a class, it checks that the bytecode is well-formed and can't corrupt memory.
  • Memory management. You create objects; the garbage collector frees the ones nothing uses anymore. There is no manual free or delete. See Memory and Garbage Collection.
  • Safety checks. Reading past the end of an array or using a missing object produces a clear exception instead of silently corrupting data.
  • Threads. The JVM runs many tasks at once on all your processor cores. See Threads.

The program below asks the JVM about itself. On your computer the answers would describe your machine; here they describe our server:

Java Ask the JVM about itself

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

The same source code, without any change, would print Windows 11 or Mac OS X on those systems. Your program asked a question; each JVM answered for its own platform.

The JIT compiler: why Java gets faster

If the JVM only interpreted bytecode, one instruction at a time, Java would be slow. Instead, modern JVMs (the standard one is called HotSpot) use a just-in-time (JIT) compiler:

  1. Code starts out interpreted, so the program starts quickly.
  2. The JVM counts how often each method and loop runs.
  3. "Hot" code that runs a lot is compiled into native machine code for your exact processor, using what the JVM has learned while watching it run.
  4. The hottest code is recompiled again with more aggressive optimizations.

This is why Java is usually described as both compiled and interpreted, and why long-running Java servers are very fast. You can watch the effect here. The program calls the same helper method, work(), eight times and measures how long each call takes:

Java Watch the JIT warm up

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

The exact numbers change on every run, but the pattern is clear: the first round is typically several times slower than the rest (on our server, often around ten times). It starts in the interpreter; by round two or three, work() and the code it calls run as compiled machine code. Every round computes the same result, so the work itself never changed. An occasional slow round later on usually means the JVM paused briefly to compile more code or to collect garbage. That's also why benchmarking Java needs care, as you'll see in Java Performance Tips.

Note: Java also has an ahead-of-time option. Tools like GraalVM Native Image compile a whole program to a native executable that starts in milliseconds. It's popular for command-line tools and cloud functions, but the standard JVM with its JIT remains the default.

JDK, JRE and JVM

These three acronyms appear everywhere:

Name What it is Who needs it
JVM (Java Virtual Machine) The engine that runs bytecode. Part of everything below.
JRE (Java Runtime Environment) A JVM plus the standard class library (strings, collections, files, networking...). Enough to run Java programs. Historically, end users.
JDK (Java Development Kit) A JRE plus developer tools: javac, javap, jshell, jar, javadoc, jlink and more. Developers. That's you.

Since Java 11, vendors no longer ship a separate JRE download. You install a JDK, and applications that need a small runtime build a custom one with jlink (covered in The Java Module System).

Java versions and LTS releases

Java gets a new feature release every six months, in March and September. Every two years, one release is designated LTS (long-term support), meaning vendors provide security fixes for many years. Most companies run LTS versions:

Version Released Highlights
Java 8 (LTS) 2014 Lambdas and streams. Still found in older systems.
Java 11 (LTS) 2018 New HTTP client, running .java files directly.
Java 17 (LTS) 2021 Records, sealed classes, text blocks.
Java 21 (LTS) 2023 Virtual threads, pattern matching for switch, record patterns.
Java 25 (LTS) 2025 Compact source files, IO class, module imports.

This course uses Java 25. If you install Java yourself, choose Java 25 (or at least 21, though then the compact void main() style won't work). Some releases also include preview features: finished designs that you can try with a --enable-preview flag before they become permanent. Java Versions and History has the full story.

Installing a JDK

You don't need to install anything for this course, but you will want Java on your own machine eventually. OpenJDK is free and open source, and several organizations publish builds of it. They are all essentially the same Java:

  • Eclipse Temurin (from the Adoptium project), a popular free default. Our server runs Temurin.
  • Oracle OpenJDK and Oracle JDK.
  • Amazon Corretto, Microsoft Build of OpenJDK, Azul Zulu and others.

Download a Java 25 JDK installer for your system, run it, and then check the installation in a terminal:

Terminal
java --version
javac --version

Both should print a version starting with 25. If they print an older version or "command not found", the installer didn't update your PATH; most installers have a checkbox for that, or you can set JAVA_HOME and add its bin folder to PATH. On macOS and Linux, tools like SDKMAN! make it easy to install and switch between several JDKs.

Running Java files

Once a JDK is installed, there are several ways to run code.

Run a source file directly. Since Java 11, the java launcher can compile a single source file in memory and run it immediately. No .class file is written:

Terminal
java Main.java

This is exactly how our editor runs your examples. Since Java 22, the launcher can also run small programs split across several .java files in the same folder, compiling the others as needed.

Compile, then run. For real projects you compile first, which also lets you run the program many times without recompiling:

Terminal
javac -d out Main.java     # compile into the "out" folder
java -cp out Main          # run class Main, looking for classes in "out"

Package as a JAR. Applications are usually shipped as .jar files (zip archives of class files) and started with java -jar app.jar. Build tools create these for you; see Build Tools: Maven and Gradle.

Experiment in JShell. The JDK includes jshell, an interactive prompt where you type Java expressions and see the result immediately:

TEXT
$ jshell
jshell> 6 * 7
$1 ==> 42
jshell> "hello".toUpperCase()
$2 ==> "HELLO"
jshell> /exit

IDEs and editors

For anything beyond small files, you'll want an IDE (integrated development environment). It highlights errors as you type, completes code, refactors safely and runs a debugger:

  • IntelliJ IDEA is the most widely used Java IDE. The Community Edition is free.
  • Eclipse and Apache NetBeans are free, open-source alternatives.
  • Visual Studio Code with the Java extensions is a lighter option.

All of them use the same JDK underneath. Whatever you choose, the code you learn here works unchanged.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does the javac compiler produce from Greeter.java?

    javac compiles source code into bytecode stored in .class files. Bytecode is the same on every platform; the JVM turns it into machine code at run time.
  2. 2. Which statement best describes "write once, run anywhere"?

    Each platform has its own JVM, but all JVMs run the same bytecode, so you compile once and ship the same class or JAR files everywhere.
  3. 3. In the JIT warm-up example, why is the first round usually the slowest?

    Every round does identical work and prints the same result. The JVM interprets code at first, notices the hot method and compiles it to native code, so later rounds speed up.
  4. 4. You want to write and compile Java programs. What do you need to install?

    The JDK contains the compiler (javac) and other developer tools on top of the runtime. Since Java 11, a separate JRE isn't even distributed.
  5. 5. After running javac Greeter.java, which command runs the program?

    The java launcher takes a class name, not a file name. Alternatively, java Greeter.java compiles and runs the source file in one step.

Summary

  • Java source (.java) is compiled by javac into bytecode (.class), which the JVM runs.
  • The same bytecode runs on any JVM, which is how Java achieves write once, run anywhere.
  • The JVM verifies code, manages memory with a garbage collector, and uses a JIT compiler to turn hot code into fast machine code.
  • The JDK = JVM + class library + tools (javac, jshell, javap, jar...). Install a JDK, not a JRE.
  • LTS versions (8, 11, 17, 21, 25) get long-term support; this course uses Java 25.
  • java Main.java runs a source file directly; javac + java compiles first; real apps ship as JARs and are built with IDEs and build tools.

Next: Java Syntax, where you'll learn the rules for statements, blocks, classes and names, and how to read compiler errors.