SLIDE 1 / 5

1. Contiguous Memory: Why Arrays Start at 0

An array in Java stores its elements side by side in physical RAM. The index is not an ordinal ranking, but an address offset. This is why accessing any random index takes strictly O(1) time.

base = 0x2000 [0] 10 0x2000 [1] 20 0x2004 [2] 30 0x2008 [3] 40 0x200C [4] 50 0x2010 [5] ¡FUERA! Each int takes exactly 4 contiguous bytes.
Memory Jump Calculator int[] numeros = new int[5];
Hardware Formula: 0x2000 + 0 * 4 bytes
Resolved RAM Address: 0x2000
Value stored: 10

Index 0 results in 0 * 4 = 0 bytes offset, pointing straight to the array start.

2. Matrices in Java: Arrays of Arrays (Jagged)

Java has no contiguous 2D grid matrix in RAM. A matrix int[][] is strictly an array containing references to independent row arrays, each able to hold a different length.

matriz[][] [0] ref [1] ref [2] ref [ 2 cols ] [ 5 columnas: irregular ] [ 3 cols ] Each row holds an autonomous .length property.
Jagged Row Inspector
Length property: 2
Stored elements: [10, 20]
Heap Reference: @0x7a20 (Independiente)

Row 0 has 2 slots. You can reassign it with new int[500] without affecting other rows.

3. String Immutability: Written in Stone

A String object in Java can NEVER be altered. Methods like .toUpperCase(), .trim(), or .replace() create a BRAND NEW object in the Heap. If unassigned, it is immediately discarded.

Stack String s1 Heap: 0x4010 "hola" Heap: 0x4080 "HOLA" State after s1.toUpperCase(); s1 still points to "hola"
Assignment Simulator
Current s1 value: "hola"
"HOLA" Heap Object: Orphaned → Sent to GC

The quintessential beginner trap! Invoking the method without catching the return leaves s1 unchanged.

4. The String Constant Pool: == vs .equals()

The JVM saves memory by storing literal strings inside the String Constant Pool. Two literals point to the same address. However, new String() forces a separate instance. This is why you must never compare strings with ==.

Stack String a String b String c Heap String Constant Pool "java" @0x5010 new String "java" @0x7890
Equality Verifier
Operation: a == b (Direcciones)
Boolean Result: true
JVM Rationale: Shared reference in Pool (@0x5010)

Both literals share the same slot. == compares memory addresses and coincidentally yields true.

5. StringBuilder: The Mutable Text Buffer

Appending inside a loop with + ("s += i") allocates a new String per iteration, stressing the Garbage Collector. StringBuilder maintains a single internal char[] buffer that dynamically expands.

String s += palabra; Heap saturado de basura N vueltas = N objetos huérfanos "a" → "ab" → "abc" → "abcd"... Complejidad O(N²) StringBuilder sb 1 Solo Objeto en Heap char[] mutable: [a|b|c|d|e|_|_] sb.append("..."); // in-situ Complejidad O(N)
Load Simulator: 1,000 Appends
Objects allocated in Heap: 1.000 objetos
Garbage Collector overhead: Critical (999 garbage objects)
Relative execution speed: ~120 ms (Lento)

Each concatenation re-copies preceding characters into a fresh array. Cost scales quadratically.