Difficulty: Beginner
What is the difference between JDK, JRE and JVM? How does Java achieve platform independence?
Let's start with the question that opens almost every Java interview in India, because if you fumble this one, the interviewer quietly lowers their expectations for the rest of the round. The good news is that it is really just three nested boxes, and once you see the nesting you will never forget it.
Think of a restaurant. The JVM is the chef who actually cooks. The JRE is the whole kitchen, meaning the chef plus the utensils and ingredients (the standard libraries) he needs. The JDK is the kitchen plus the tools used to design and build new recipes, like the compiler and the debugger. So JDK contains JRE, and JRE contains JVM.
The JVM (Java Virtual Machine) is a specification and an implementation that executes bytecode. It loads classes through the class loader subsystem, verifies them, manages runtime memory, interprets bytecode and, through the JIT (Just-In-Time) compiler, compiles hot methods into native machine code while the program runs. HotSpot is the JVM most people use. The JVM is platform specific: there is one build for Windows, one for Linux, one for macOS, and so on.
The JRE (Java Runtime Environment) is the JVM plus the core class libraries such as java.lang, java.util and java.io, plus supporting files. It is everything you need to run a Java program, but not to write one. Since Java 11, Oracle and OpenJDK no longer ship a standalone JRE download; you either install a JDK or build a trimmed runtime with jlink. Still, interviewers ask about the JRE, so know the concept.
The JDK (Java Development Kit) is the full toolbox. It includes the JRE and development tools: javac (compiler), java (launcher), jar, javadoc, jdb, jshell and jcmd. If you want to compile code, you need a JDK.
Now the famous part: platform independence. Java follows write once, run anywhere. When you run javac on Hello.java, it does not produce machine code for your CPU. It produces bytecode in Hello.class, an intermediate instruction set that is the same on every machine. Then each operating system has its own JVM that knows how to translate that bytecode to the native instructions of that platform. So the bytecode is portable, but the JVM is not. Precisely speaking, Java the language and bytecode are platform independent, while the JVM is platform dependent.
Compare this with C. A C program is compiled directly to a machine-specific executable, so a Windows binary will not run on Linux without recompiling. In Java the same .class file runs on both.
Two edge points interviewers like. First, the JVM does not only run Java: Kotlin, Scala and Groovy compile to bytecode and run on the same JVM. Second, platform independence is not free; there is the startup cost of the JVM and the interpretation before JIT kicks in, though for long-running servers the JIT often produces code competitive with native.
A related follow-up is why Java is called both compiled and interpreted. It is compiled to bytecode by javac, then interpreted or JIT-compiled by the JVM at runtime. Both statements are true, and saying so shows depth.
Finally, remember that the version of the class file matters. A class compiled by JDK 21 will not run on a JVM 11 and throws UnsupportedClassVersionError, because the JVM refuses bytecode newer than it understands. You can compile with the release flag to target older versions.
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, InternHack");
}
}
// javac Hello.java -> Hello.class (bytecode, same on all platforms)
// java Hello -> JVM of the current OS runs it
// javap -c Hello -> inspect the bytecode
The output is the same on every OS. The point is that Hello.class is identical everywhere; only the JVM that executes it differs.
JDK, JRE, JVM, Bytecode, Platform Independence