Commit 5a4edab
committed
feat: Explain why strings created with
WHAT the explanation covers:
- Defines heap memory as the region used for **dynamic memory allocation**, where objects live during runtime.
- Explains that when you create a String using `new String("...")`, the JVM allocates the object on the heap.
- Clarifies why:
1. Heap supports objects with variable size, and strings are variable-length sequences of characters.
2. Heap memory is managed by the JVM’s garbage collector, freeing unused objects automatically.
3. All objects in Java — including Strings created with `new` — are always allocated on the heap (not on the stack).
WHY this matters:
- Helps developers understand Java’s memory model:
- **Stack**: stores method calls, local variables, and references.
- **Heap**: stores objects and arrays, which can outlive the method that created them.
- Understanding where objects live is crucial for reasoning about performance, garbage collection, and object lifecycle.
- Distinguishes between `String str = "Hello";` (interned string in the **string pool**) and `String str = new String("Hello");` (new object on the **heap**, outside the pool).
HOW it works:
1. `String str1 = "Hello";`
- JVM checks the **string pool** in the heap.
- If "Hello" exists, returns a reference to it; otherwise, creates it in the pool.
- str1 references the pooled string.
2. `String str2 = new String("Hello");`
- Always creates a **new object** in the heap, even if "Hello" already exists in the pool.
- str2 references a distinct object, different from str1.
3. Both str1 and str2 reside in the heap, but str1 points to a pooled instance while str2 points to a new one.
Tips and gotchas:
- Using `new String("...")` is generally discouraged unless you explicitly need a new, distinct object (rare).
- String pool optimizes memory and improves performance by reusing common literals.
- Garbage collector cleans up heap objects when no references remain, but interned string literals remain for the program’s lifetime.
- Stack never stores objects themselves — only references to heap objects.
Use-cases / analogies:
- Think of the **stack** as a desk where you keep quick notes (method calls and references).
- The **heap** is like a warehouse where actual bulky items (objects like Strings) are stored.
- String pool is a “shared shelf” in the warehouse — if one worker has already placed "Hello" there, others reuse it instead of making duplicates.
Short key: java-memory heap stack string-pool new-operator garbage-collection.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>new in Java are allocated in heap memory1 parent a6a9f2c commit 5a4edab
1 file changed
+18
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
0 commit comments