Fundamentals 4 min read

Understanding Java Heap and Stack Memory with Examples

This article explains the differences between Java heap and stack memory, their advantages and disadvantages, and demonstrates how string objects are stored and shared using code examples that illustrate memory allocation and reference behavior.

Java Captain
Java Captain
Java Captain
Understanding Java Heap and Stack Memory with Examples

In Java, both heap and stack are areas in RAM used to store data during program execution.

Heap : It is a runtime data area where class objects are allocated via new and reclaimed by the garbage collector. The heap allows dynamic memory allocation, which provides flexibility but incurs slower access speed.

Stack : It primarily stores primitive type variables (byte, short, int, long, float, double, boolean, char) and object references. Stack access is fast and data can be shared, but the size of each stack frame must be determined at compile time, limiting flexibility.

To illustrate stack data sharing, consider two ways to create a String in Java.

First way (using new):

String str1 = new String("abc");
String str2 = "abc";

The object created with new resides in the heap, and each call creates a new object.

Second way (string literal): the JVM checks the string pool (often associated with the stack) for an existing "abc". If it exists, the reference points to that single object; otherwise, the literal is added to the pool.

Code example demonstrating the first approach:

public static void main(String[] args) {
    String str1 = new String("abc");
    String str2 = new String("abc");
    System.out.println(str1 == str2);
}

Output: false because two distinct objects are created on the heap.

Code example demonstrating the second approach:

public static void main(String[] args) {
    String str1 = "abc";
    String str2 = "abc";
    System.out.println(str1 == str2);
}

Output: true because both references point to the same interned string in the pool.

Thus, using string literals can save memory and improve performance, as the JVM reuses existing objects instead of allocating new ones each time.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaMemory ManagementGarbage CollectionStackStringHeap
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.