🚀JAVA String Pool


🔤 Java String Pool Explained (With Simple Examples)

Understanding how Java handles Strings is very important for memory efficiency. One of the core concepts here is the String Pool.

Let’s break it all down in a beginner-friendly way.


đź§  Java Memory Overview

Java memory is mainly divided into two parts:

  • 🗂️ Stack – Stores primitive values (like int, boolean) and references to objects
  • 📦 Heap – Stores actual objects, like instances of classes, arrays, etc.

🔹 What is the String Pool?

The String Pool is a special area inside the heap memory where Java stores String literals.

âś… Key Points:

  • When a String is created using double quotes, Java checks if that exact String already exists in the pool.
  • If it exists, Java reuses it.
  • If not, Java adds it to the pool.
  • This saves memory by avoiding duplicates.

đź§ľ What is a String Literal?

A String literal is any text enclosed in double quotes.

String name = "Yashith";

Here, "Yashith" is a String literal and gets stored in the String Pool.


🔍 String Pool in Action – Examples

Let’s look at some real code examples to see how String Pool works:

String a = "Hello";
String b = "Hello";
String c = new String("Hello");

đź’ˇ What happens here?

  • a refers to "Hello" in the String Pool.
  • b sees "Hello" already in the pool, so it shares the same object.
  • c creates a new object in the heap, even though "Hello" exists in the pool.

🧪 Let’s test it:

System.out.println(a == b);      // true  âś… same object (pooled)
System.out.println(a == c);      // false ❌ different object
System.out.println(a.equals(c)); // true  âś… same content

⚠️ == vs .equals() – What’s the Difference?

âť—== compares references (memory locations)

âś… .equals() compares the text content

Example:

String x = "Java";
String y = new String("Java");

System.out.println(x == y);        // false — different objects
System.out.println(x.equals(y));   // true — same content

Always use .equals() to compare actual String values/text!


đź§  One More Example: Compile-Time Optimization

String x = "World";
String y = "World";
String z = "Wor" + "ld"; // Compiler joins and optimizes this

System.out.println(x == y); // true
System.out.println(x == z); // true

Java compiler optimizes string literals created at compile time, and stores them in the pool too!


📌 Summary

ConceptExplanation
String LiteralText in double quotes like "Hello"
String PoolSpecial area in heap to store and reuse string literals
==Compares memory/reference (are they the same object?)
.equals()Compares actual content/text inside the string
new String()Always creates a new object in heap (even if same text exists in pool)

âś… Pro Tip:

If you want to force a String into the pool manually, use:

String newStr = new String("Java").intern();

This makes newStr refer to the pooled version of "Java".


đź’¬ Final Thoughts

Using the String Pool properly can help your Java application use memory more efficiently and avoid creating unnecessary objects. Just remember:

  • Use literals whenever possible.
  • Use .equals() when comparing Strings.
  • Avoid using new String("...") unless absolutely necessary.

Leave a Reply

Your email address will not be published. Required fields are marked *