1. JVM Memory Model
- Stack — one frame per method call; holds local variables, primitive values, and references to heap objects. Stack lives and dies with the method call.
- Heap — the single shared area where all objects and arrays live; shared by all threads. Managed by the garbage collector.
- Metaspace (Java 8+) — stores class metadata and static variables; replaced PermGen. No size cap by default.
- Method area — a logical part of Metaspace holding class definitions, constant pools, and method bytecode.
- Gotcha: local primitives are on the stack, but any object (including Integer, String) is on the heap — only the reference sits on the stack.
2. Primitives vs Reference Types
| Primitive | Size | Default |
|---|---|---|
byte | 1 byte | 0 |
short | 2 bytes | 0 |
int | 4 bytes | 0 |
long | 8 bytes | 0L |
float | 4 bytes | 0.0f |
double | 8 bytes | 0.0d |
char | 2 bytes (unsigned) | '\u0000' |
boolean | 1 bit (JVM-dep) | false |
- Primitives are value types — copied by value, compared with
==. - Reference types are objects — the variable holds an address;
==on references compares addresses unless overridden (or for the special wrapper cases below). - Exact comparison trap:
==on two Integer objects compares references, but JVM caches Integer in[-128, 127]— soInteger a=100, b=100; a==bistrue, butInteger c=200, d=200; c==disfalse. Always use.equals()for wrappers.
Boxing & unboxing
- Autoboxing wraps a primitive into its wrapper (
int → Integer); unboxing unwraps it. Integer i = null; int x = i;throws NullPointerException at unboxing.
3. Operators & Precedence
==vs.equals():==compares references (or primitives),.equals()compares logical content (overridden in classes like String, wrapper, Collections).&&vs&:&&short-circuits;&also evaluates the right side (and can be bitwise).||vs|: same rule —||short-circuits.++i(pre) vsi++(post): pre increments first, then returns; post returns the old value first, then increments. Inint y = i++ + i++;evaluation is left-to-right with the reads — classic output trap.instanceof: tests whether a reference is a subtype or implementation.- Ternary
? :evaluates only the chosen branch (unlike&/|which evaluate both).
Precedence (high → low): ++/--/unary > () method call > *///%, +/- > shift / > relational </>/<=/>= > equality ==/!= > & > ^ > | > && > || > ternary > assignment. Rely on parentheses in code — precedence questions are the #1 trap.
4. Control Flow
switchonint/char/String/enum(and wrapper types) — cases are constants;breakprevents fall-through (Java 12+ switch expressions with arrows/yield).forvs enhancedfor: enhancedfor (int v : list)internally uses the iterator; removing while iterating throws ConcurrentModificationException unless you useiterator.remove()orListIterator.try/catch/finally:finallyalways runs (unlessSystem.exitor JVM crash).returninsidetryexecutes, thenfinally, then returns — areturninfinallyoverrides the try-return.- Labeled loops
outer:—break outer;exits the outer loop.
Gotcha: floating-point equality in switch is not allowed; comparing floats with == after arithmetic is unreliable.
5. Strings at Interview Speed
- String pool (interned): literals like
"hi"are interned;new String("hi")creates a heap object not in the pool (unless.intern()). - String equals: content —
.equals()overridden, sonew String("a").equals("a")→true, butnew String("a") == "a"→false. - String immutability — reasons: thread-safety, hash caching, security, classloader safety.
StringBuilder: mutable, unsynchronized (fast — use in a single thread);StringBuffer: synchronised (slow, for multithreaded).String.concat()vs+:+is compiled toStringBuilderunder the hood, so chained+in a loop creates wasted builders — use explicitStringBuilderin loops.
String a = "ab";
String b = "ab";
System.out.println(a == b); // true (same interned literal)
String c = new String("ab");
System.out.println(a == c); // false (different reference)
System.out.println(a.equals(c)); // true (content)
final String x = "a"; // compile-time constant
String y = x + "b"; // interns like "ab"
System.out.println(y == a); // truePremium Content
Unlock Part 1: JVM Memory, Types & Fundamentals and all premium lessons with a subscription.
All premium lessons
Ad-free experience
Priority support
From ₹199.99/year — See plans