← Back to list

How exactly String Pool Works in Java?

Let me deep dive in to make you understand back story.

Kumar Saurav · 2026-06-15 06:45 · 4 claps · 3.8 min read paywalled
#java #string-pool #jvm #programming #memory-optimization
Open on Medium ↗
Wiki topics: 💻 · Programming

How exactly String Pool Works in Java?

Let me deep dive in to make you understand back story.

Have you ever wondered why these two lines return **true**?

String a = "java";
String b = "java";
System.out.println(a == b);

And then this one returns **false**?

String a = new String("java");
String b = new String("java");
System.out.println(a == b);

Same text. Different result. Same language. Different behavior.

So what is Java really doing behind the scenes?

The answer is the String Pool.

Think of it like a shared shelf

Imagine a library where popular books are kept on a special shelf.

If ten people ask for the same book, the library does not print ten separate copies. It gives everyone a reference to the same book.

That is exactly how Java handles string literals.

When you write:

String s1 = "hello";

Java first checks the String Pool.

  • If "hello" already exists there, Java reuses it.
  • If it does not exist, Java creates it and stores it in the pool.

So when you write:

String s2 = "hello";

s2 points to the same pooled object as s1.

What exactly is the String Pool?

The String Pool is a special memory area inside Java where string literals are stored and reused.

This helps Java save memory because identical string values do not need to be stored again and again.

For example:

String a = "java";
String b = "java";
String c = "java";

All three variables can point to the same object in the pool.

This is one of the reasons strings are such an important topic in Java interviews.

Why does Java do this?

Because strings are used everywhere.

If Java created a new object every time the same text appeared, memory usage would grow very quickly.

The pool helps Java:

  • Save memory,
  • Re-use common string values, and improve performance in many cases.

Why does this work safely?

Because strings in Java are immutable.

Immutable means once a string is created, it cannot be changed.

That is the key.

If strings were mutable, sharing the same object between multiple references would be dangerous. One change could affect everyone using that object.

Since strings cannot change, Java can safely reuse them.

== vs equals()

This is where most people get confused.

==

Checks whether two references point to the same object.

equals()

Checks whether two strings have the same content.

Example:

String x = "java";
String y = new String("java");
System.out.println(x == y);      // false
System.out.println(x.equals(y)); // true

Why?

  • x points to the pooled string
  • y points to a new object in the heap
  • both contain the same characters, so equals() returns true

So when comparing strings, equals() is usually the correct choice.

What happens step by step?

Consider this code:

String s1 = "apple";
String s2 = "apple";
String s3 = new String("apple");

Step 1: String s1 = "apple";

Java checks the pool.

  • "apple" is not there
  • Java creates it in the pool
  • s1 points to that pooled object

Step 2: String s2 = "apple";

Java checks the pool again.

  • "apple" is already there
  • s2 points to the same pooled object as s1

Step 3: String s3 = new String("apple");

This creates a new object in heap memory.

  • s3 points to a separate object
  • the literal "apple" still exists in the pool too

So now:

s1 ---> pooled "apple"
s2 ---> pooled "apple"
s3 ---> new heap object "apple"

What does new String() really do?

This is important.

When you write:

String s = new String("hello");

Java creates:

  1. the literal "hello" in the pool if it is not already there,
  2. and a new String object in heap memory.

So new String(...) gives you a fresh object even if the same value already exists in the pool.

That is why this happens:

String a = "hello";
String b = new String("hello");
System.out.println(a == b); // false
System.out.println(a.equals(b)); // true

What does intern() do?

intern() is the method that connects a string to the pool.

String s = new String("java");
String t = s.intern();

It asks Java:

“Does this exact string already exist in the pool?”

  • If yes, Java returns the pooled reference.
  • If no, Java adds it to the pool and returns that reference.

Example:

String a = new String("java");
String b = a.intern();
System.out.println(a == b); // false

Here:

  • a is the heap object
  • b points to the pooled "java"

Another example:

String x = "java";
String y = new String("java").intern();
System.out.println(x == y); // true

Now both point to the same pooled object.

A simple memory picture

String Pool
+------------------------+
| "hello"  ---> object A |
| "java"   ---> object B |
+------------------------+
Heap
+------------------------+
| new String("hello")    |
| new String("java")     |
+------------------------+

This is the easiest way to remember it:

  • String literals go to the pool
  • new String() creates a new object in heap
  • intern() helps move or link to the pool

The most important rule to remember

If two strings look the same, they are not always the same object.

Use:

  • == to compare references
  • equals() to compare content

Example:

String a = "data";
String b = new String("data");
System.out.println(a == b);      // false
System.out.println(a.equals(b)); // true

This is one of the most common mistakes Java developers make early on.

Why the String Pool matters so much

The String Pool is not just a theory topic.

It matters because it helps you understand:

  • memory optimization,
  • object references,
  • string immutability,
  • == vs equals(),
  • intern(),
  • and how Java manages objects under the hood.

In interviews, this topic often appears as a small question with a big hidden depth.

Final takeaway

The String Pool is Java’s way of reusing string literals instead of creating duplicates.

That makes Java more memory-efficient.

The full picture is simple:

  • string literals are stored in the pool,
  • identical literals point to the same object,
  • new String() creates a separate heap object,
  • equals() checks content,
  • == checks reference,
  • intern() brings a string into the pool.

Once this clicks, the whole concept becomes much easier.

Java is not behaving strangely here. It is just being efficient.

:) 😊✌️ Happy Coding….


메타데이터
post_id
151dfee488cd
slug
how-exactly-string-pool-works-in-java-151dfee488cd
url
https://medium.com/@sauravsku/how-exactly-string-pool-works-in-java-151dfee488cd
canonical_url
https://medium.com/@sauravsku/how-exactly-string-pool-works-in-java-151dfee488cd
author_url
https://medium.com/@sauravsku
status
ok
fetched_at
2026-07-14 00:09:08