JDK, JRE, JVM and Platform Independence Questions
45 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)Almost every fresher interview opens with "JDK vs JRE vs JVM", platform independence and "is Java compiled or interpreted?". The interviewer is checking that you picture how a Java program really runs, so a crisp answer sets the tone for the round.
Recap: JDK contains JRE contains JVM
The JDK (Java Development Kit) is everything needed to build and run Java programs. It contains the JRE plus development tools such as javac, javap, jar and jshell. The JRE contains the JVM and the libraries.
Three nested boxes answer the question. This diagram shows what sits inside what.
Example 1 · What sits inside what
+------------------------------- JDK --------------------------------+| javac javap jar jshell jlink ... (development tools) || +---------------------------- JRE ------------------------------+ || | java.lang, java.util, java.io ... (class libraries) | || | +------------------------- JVM ----------------------------+ | || | | class loader -> bytecode verifier -> interpreter + JIT | | || | | heap, stacks, garbage collector | | || | +----------------------------------------------------------+ | || +---------------------------------------------------------------+ |+--------------------------------------------------------------------+JDK = JRE + tools. JRE = JVM + libraries. JVM = runs bytecode.
What this code does
- Draw this box diagram on a whiteboard when asked; it answers "difference between JDK, JRE and JVM" in ten seconds.
- Mention that since Java 11 there is no separate Oracle JRE download: the box still exists as an idea, and
jlinkcan build a small runtime image.
The JVM loads .class files and runs their bytecode. The JRE is the JVM plus standard libraries such as java.lang and java.util. Since Java 11 Oracle ships no separate JRE, and jlink can build a small runtime. javac compiles; java starts a JVM and runs main.
Revise: JDK, JRE and JVM (opens in a new tab) and The JVM big picture (opens in a new tab).
Why Java is platform independent and the JVM is not
Bytecode is the instruction set of the JVM, stored in .class files. The Java compiler (javac) produces it from source code. It is not tied to any real CPU, so the same bytecode runs on any platform with a JVM.
Platform independence means the same .class file runs on every system. This diagram shows one bytecode file running on three different JVMs.
Example 2 · One .class file, many JVMs
Hello.java | javac (once) v Hello.class (bytecode: same bytes everywhere) / | \ v v v JVM for JVM for JVM for Windows x64 Linux x64 macOS ARM64 | | | Windows API Linux calls macOS calls -> same output: Hello, placement!Bytecode is portable; each JVM is built for its own platform.
What this code does
- The middle row is the platform-independent part: one
.classfile, copied unchanged to every machine. - The JVM row is platform dependent: three different JVM programs that all understand the same bytecode.
Platform independence works because javac produces bytecode, not instructions for a real CPU, and each platform has its own JVM that understands the same bytecode. The JVM itself is platform dependent: it turns bytecode into real machine instructions and calls the operating system for files, threads and memory. Model answer: "Java is portable at the bytecode level because a platform-specific JVM does the platform work."
Compiled or interpreted? Fully object-oriented?
The JIT (Just-In-Time) compiler is the part of the JVM that turns frequently run bytecode into native machine code while the program runs. That is why Java gets faster after a short warm-up.
Java is not 100% object-oriented, because primitives are plain values. This program shows a primitive next to its wrapper object.
Example 3 · Primitives and static methods: why Java is not pure OOP
public class NotPureOop { public static void main(String[] args) { int marks = 91; // primitive: a plain value with no methods Integer boxed = marks; // wrapper object (autoboxing)
System.out.println(boxed.getClass().getName()); System.out.println(boxed.toString().length()); System.out.println(Math.max(marks, 75)); // static method: no object needed }}java.lang.Integer 2 91
What this code does
marks.toString()would not compile, becauseintis a primitive with no methods. The wrapperIntegeris a real object of classjava.lang.Integer.Math.maxis called on the class, with no object at all. Primitives and static members are the two standard reasons Java is not purely object-oriented.
Java is both compiled and interpreted: javac compiles source to bytecode first, then the JVM interprets bytecode and its JIT compiles hot methods. It is not 100% object-oriented: the eight primitive types are plain values and static members belong to a class without any object. Safe answer: "Java is strongly object-oriented, but not pure."
Predict the output
Predict: object or primitive?
Read each snippet and pick what it prints.
Round 1 of 3
The javac and java flow, and what a .class file holds
The class loader is the part of the JVM that finds a .class file, reads it into memory and prepares it for use. The JVM loads each class the first time the program needs it.
The two commands javac and java do different jobs. This diagram and program show the flow and what a .class file holds.
Example 4 · Looking inside a .class file with javap
javac --release 17 Hello.javajavap -c HelloCompiled from "Hello.java"
public class Hello {
public Hello();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: getstatic #7 // Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #13 // String Hello, placement!
5: invokevirtual #15 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
8: return
}What this code does
Hello.javais a one-line program that printsHello, placement!.javap -cshows its bytecode:getstaticfetchesSystem.out,ldcloads the string from the constant pool,invokevirtualcallsprintln.- Notice
public Hello(): the compiler added a default constructor that callsObject's constructor. The#7,#13numbers point into the constant pool.
Example 5 · Reading the magic number and version of a class file
import java.io.DataInputStream;import java.io.IOException;import java.io.InputStream;
public class ClassFileHeader { public static void main(String[] args) throws IOException { try (InputStream raw = ClassFileHeader.class.getResourceAsStream("ClassFileHeader.class"); DataInputStream in = new DataInputStream(raw)) { int magic = in.readInt(); int minor = in.readUnsignedShort(); int major = in.readUnsignedShort(); System.out.printf("Magic number: %X%n", magic); System.out.println("Version: " + major + "." + minor + " (Java " + (major - 44) + ")"); } }}Magic number: CAFEBABE Version: 61.0 (Java 17)
What this code does
- The program reads its own
.classfile: the first four bytes are alwaysCAFEBABE, followed by the minor and major version numbers. - This output is for
javac --release 17. Compiled by JDK 21 without--release, it shows 65.0 (Java 21); a Java 17 JVM would then refuse it withUnsupportedClassVersionError.
javac Hello.java checks the source and writes Hello.class. java Hello starts a JVM, which loads the class, verifies the bytecode and calls main. One .java file can hold several classes, each becoming its own .class file. A class file starts with the magic number CAFEBABE and has a version (61 for Java 17, 65 for Java 21), a constant pool of names and literals, and each method's bytecode.
Revise: Inside a class file (opens in a new tab) and What is class loading (opens in a new tab).
Order of execution
From Hello.java to output
Put the steps in the order they happen.
Traps interviewers listen for
A few answers lose marks instantly because they show a shaky picture of how Java runs. This block shows three commands, then you spot the wrong claim in three short answers.
javac -version # needs a JDK: a runtime-only install has no compilerjava -version # works with a JDK or a runtimejava Hello.java # Java 11+: compiles in memory and runs one source fileTypical wrong answers: "JRE and JVM are the same" (the JRE is the JVM plus the libraries), "Java is 100% OOP", "Java is only interpreted", and "the JVM is platform independent" (the bytecode is; the JVM is per platform). Interviewers often ask a follow-up just to see whether you fall into one of them.
Find the bug
Spot the wrong claim
Each answer has one wrong line. Tap it, then check the correction.
A candidate explains JDK, JRE and JVM.
Round 1 of 3Next step
Check your understanding
12 quick questions