Q1 · Java basics and types
What is the difference between the JDK, the JRE and the JVM?
The JVM (Java Virtual Machine) runs compiled Java bytecode. The JRE (Java Runtime Environment) is the JVM plus the standard class library, which is everything you need to run a program. The JDK (Java Development Kit) is the JRE plus tools for developers, such as the compiler javac, jshell and jar. Since Java 11 you normally just install a JDK.
In simple words: The JVM runs the code, the JRE is what you need to run it, and the JDK is what you need to write it.
Why it works
Java source code is not turned straight into machine code for your processor. javac first compiles .java files into .class files that contain bytecode, a compact set of instructions for an imaginary machine. The JVM reads that bytecode and runs it, first by interpreting it and then, for code that runs often, by compiling it to real machine code while the program runs. That second step is called JIT compilation (just-in-time). This is why the same .class file runs on Linux, Windows and macOS: each platform has its own JVM.
Older Java versions shipped a separate JRE download for people who only wanted to run programs. From Java 11 onwards, vendors such as Oracle, Eclipse Temurin and Amazon Corretto ship the JDK, and you can build a small custom runtime with the jlink tool if you need one. So in interviews you can say "JRE" for the idea (the runtime part) even though it is rarely a separate product now.
Since Java 11 you can also run a single source file directly with java Hello.java. The launcher compiles it in memory and runs it. Almost every snippet in this pack was run that way. The few that end in a NullPointerException were compiled with javac -g and then run with java, because -g keeps local variable names in the class file, so the error message can name the variable that was null.
class Main {
public static void main(String[] args) {
Runtime.Version v = Runtime.version();
System.out.println("Feature release: " + v.feature());
System.out.println("Spec version: "
+ System.getProperty("java.specification.version"));
System.out.println("Bytecode class version for Java 21: "
+ (44 + 21));
}
}Output, real run on Java 21.0.13:
Feature release: 21
Spec version: 21
Bytecode class version for Java 21: 65The last line is a small fact worth knowing: class file version 65 means Java 21. If you run a class compiled for a newer Java on an older JVM, you get an UnsupportedClassVersionError that mentions these numbers.
Common mistakes
- Saying "Java is interpreted". The JVM starts by interpreting bytecode but compiles hot code to machine code with the JIT compiler, so long-running Java code runs at native speed.
- Mixing up javac (compiler, in the JDK) and java (launcher that starts a JVM).
- Saying the JVM understands .java files. It only runs bytecode; the single-file launcher compiles the source for you first.
Follow-ups
- What does "write once, run anywhere" mean? The same bytecode runs on any platform that has a JVM, so you do not recompile for each operating system.
- What is the JIT compiler? The part of the JVM that compiles frequently used bytecode into machine code at run time, using what it learned while the program ran.
- What are HotSpot and GraalVM? (Advanced) HotSpot is the JVM inside most OpenJDK builds. GraalVM is another JDK distribution with its own JIT compiler and an option to compile programs ahead of time into native executables.
