Notes › Java
JDK, JRE, JVM and Java Memory
How a Java program runs
Hello.java ──javac──► Hello.class (bytecode) ──JVM──► machine code for your OS/CPU
Bytecode is the same on every platform; each OS has its own JVM. That is "write once, run anywhere."
| What it is | Contains | |
|---|---|---|
| JVM (Java Virtual Machine) | runs bytecode | class loader, bytecode verifier, interpreter, JIT compiler, garbage collector |
| JRE (Java Runtime Environment) | what you need to run Java | JVM + core libraries |
| JDK (Java Development Kit) | what you need to develop Java | JRE + javac, java, jar, javadoc, debugger |
Since Java 11 there is no separate JRE download — you install the JDK.
Inside the JVM
- Class loader — loads
.classfiles when first needed (bootstrap → platform → application loaders). - Bytecode verifier — checks the code is safe (no illegal memory access).
- Execution engine — the interpreter runs bytecode; the JIT compiler compiles hot methods to native code for speed.
- Garbage collector — frees objects that are no longer reachable.
Memory areas
| Area | Stores | Shared? |
|---|---|---|
| Stack (one per thread) | method frames: local variables, primitives, references | no |
| Heap | all objects and arrays | yes, by all threads |
| Metaspace (method area) | class metadata, static variables | yes |
| PC register | address of the current instruction | per thread |
public static void main(String[] args) {
int x = 10; // x lives on the stack
Student s = new Student("Asha"); // reference s on the stack, the object on the heap
}
- Infinite recursion →
StackOverflowError. - Too many live objects →
OutOfMemoryError: Java heap space.
Garbage collection
An object becomes eligible for GC when nothing references it any more.
Student s = new Student("Ravi");
s = null; // the old object is now unreachable → eligible for GC
- The heap is split into a young generation (new objects; collected often, quickly) and an old generation (long-lived objects).
- Modern collectors: G1 (default), ZGC, Shenandoah — designed for short pauses.
System.gc()only requests a collection; the JVM may ignore it.finalize()is deprecated — usetry-with-resourcesto release resources.
String pool
String literals are stored once in the String pool (on the heap) and reused.
String a = "java";
String b = "java";
String c = new String("java");
System.out.println(a == b); // true — same pooled object
System.out.println(a == c); // false — new object
System.out.println(a.equals(c)); // true — same characters
Rule: compare strings with .equals(), never ==.
Pass by value
Java is always pass by value. For objects, the reference is copied — so a method can change the object's fields, but reassigning the parameter does not affect the caller.
void rename(Student s) { s.name = "New"; } // caller sees the change
void replace(Student s) { s = new Student("X"); } // caller does not
Interview one-liners
- JDK vs JRE vs JVM: develop vs run vs execute bytecode.
- Why is Java platform independent? Bytecode + a JVM for each platform. (The JVM itself is platform dependent.)
- JIT compiles frequently used code to native code at run time.
- Stack vs heap: per-thread method data vs shared objects.
- Is Java 100% object-oriented? No — it has primitive types (
int,char…). - Related: Java OOP Concepts, Java Collections Framework.