Getting everything ready for you…

Loading courses, problems and your progress. This takes just a moment.

0s

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

  1. Class loader — loads .class files when first needed (bootstrap → platform → application loaders).
  2. Bytecode verifier — checks the code is safe (no illegal memory access).
  3. Execution engine — the interpreter runs bytecode; the JIT compiler compiles hot methods to native code for speed.
  4. 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
}

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

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

More Java notes