What the JVM actually is

JVM · lesson 1 of 34 · 3 min read

A program that reads bytecode and turns it into fast machine code on whatever box it lands on.

Open this lesson in the learning hub

Key points

  • Java compiles to bytecode, not machine code. The JVM reads that bytecode and produces instructions for the CPU it is running on.
  • That is the trade: one build artefact for every platform, paid for with a runtime that has to start up first.
  • The JVM is far more than an interpreter. It loads classes, manages memory, and recompiles hot code while your program runs.
  • JVM is the specification, JDK is the toolkit that builds for it, and HotSpot is the implementation you almost certainly use.
  • Any language that emits valid bytecode gets all of this for free. Kotlin, Scala and Clojure ride on exactly the same VM.

Example

public class Main {
    public static void main(String[] args) {
        Runtime rt = Runtime.getRuntime();
        System.out.println("java.version   : " + System.getProperty("java.version"));
        System.out.println("java.vm.name   : " + System.getProperty("java.vm.name"));
        System.out.println("java.vm.vendor : " + System.getProperty("java.vm.vendor"));
        System.out.println("os / arch      : " + System.getProperty("os.name")
                + " / " + System.getProperty("os.arch"));
        System.out.println("cpus visible   : " + rt.availableProcessors());
        System.out.println("max heap (MB)  : " + (rt.maxMemory() >> 20));
        System.out.println();
        System.out.println("Same bytecode, any of these VMs. That is the whole point.");
    }
}

Java is portable because the JVM is not.

This is a reading copy. The full lesson — with the visual explainer, the interactive lab and a Run button for the code — lives in the JVM course, and every lesson in it is listed on the JVM contents page.