← Back to Questions
Java

What is string pool in Java?

Learn What is string pool in Java? with simple explanations, real-time examples, interview tips and practical use cases.

The String Pool in Java is a special memory area inside the heap where Java stores String literals to optimize memory usage and improve performance.

In simple words:

Java reuses String objects from the String Pool instead of creating duplicate objects.


Why String Pool is Important?

String Pool helps:

  • Save memory
  • Improve performance
  • Reduce duplicate objects
  • Improve application efficiency
  • Support fast String comparisons

String Pool Architecture Diagram


Heap Memory

     |
     +-------> String Pool

                     |
                     +-------> "Java"

                     |
                     +-------> "Spring"

                     |
                     +-------> "Microservices"


How String Pool Works?

Whenever a String literal is created, JVM checks whether it already exists in String Pool.


If String Exists

  • Existing reference is reused

If String Does Not Exist

  • New String object is created in pool

Basic Example

String s1 = "Java";

String s2 = "Java";

Memory Representation


String Pool

     |
     +-------> "Java"

                 ^
                 |

           s1 and s2


Observation

Only one object is created for:

"Java"

Why?

Because String literals are reused from pool.


String Comparison Example

System.out.println(s1 == s2);

Output


true


Why Output is true?

Both references point to same pooled object.


String Pool Internal Working


String Literal Created

      |
      v

JVM Checks Pool

      |
      +-------> Exists?
                     |
         +-----------+-----------+
         |                       |
        Yes                     No
         |                       |
         v                       v

Reuse Existing Object     Create New Object


Using new Keyword

Using:

new String()

creates object outside String Pool.


Example

String s1 = new String("Java");

String s2 = new String("Java");

Memory Representation


Heap Objects

     |
     +-------> Object 1 → "Java"

     |
     +-------> Object 2 → "Java"


Observation

Two separate objects are created.


Comparison Example

System.out.println(s1 == s2);

Output


false


Why?

Different objects exist in heap memory.


String Literal vs new String()

Feature String Literal new String()
Memory Area String Pool Heap
Object Reuse Yes No
Memory Efficient Yes No
Performance Faster Slower

What is intern() Method?

The:

intern()

method moves String into String Pool.


Example

String s1 =
    new String("Java");

String s2 =
    s1.intern();

Memory Flow


Heap Object

      |
      v

intern()

      |
      v

Reference Returned from Pool


Example with intern()

String s1 =
    new String("Java");

String s2 = "Java";

System.out.println(
    s1.intern() == s2
);

Output


true


Why String Pool Exists?

Because Strings are heavily used in Java applications.


Examples

  • Database URLs
  • SQL queries
  • API endpoints
  • Configuration values
  • File paths
  • JSON keys

Without String Pool

Large applications would create millions of duplicate String objects.


Memory Optimization Flow


Duplicate Strings

      |
      v

Stored Once in Pool

      |
      v

Memory Saved


Why String Pool Requires Immutability?

String objects are immutable.


Why Important?

Shared pooled objects must remain unchanged.


Danger if String was Mutable


s1 = "Java"

s2 = "Java"

If s1 modifies object, s2 also changes unexpectedly.


String Pool and == Operator

The:

==

operator compares references.


Example

String s1 = "Java";

String s2 = "Java";

System.out.println(s1 == s2);

Output


true


equals() vs ==

Feature == equals()
Compares Memory Reference Content
String Pool Impact Yes No
Recommended for String Content No Yes

Example

String s1 =
    new String("Java");

String s2 =
    new String("Java");

System.out.println(
    s1.equals(s2)
);

Output


true


Why?

equals() compares content, not references.


String Pool in Banking Systems

Banking applications use String Pool for:

  • Transaction statuses
  • Account types
  • Database configurations
  • Security roles

Example

String status = "SUCCESS";

String Pool in E-Commerce Systems

E-commerce systems use String Pool for:

  • Order statuses
  • Product categories
  • Payment states
  • Shipping types

String Pool in Spring Boot

Spring Boot applications use String Pool for:

  • Configuration properties
  • Bean names
  • API routes
  • Environment variables

Spring Boot Example

@Value("${db.url}")
private String databaseUrl;

String Pool in Microservices

Microservices architectures use String Pool for:

  • Service names
  • Kafka topics
  • Cloud configurations
  • Distributed tracing IDs

String Pool Memory Flow


Application Starts

      |
      v

String Literals Loaded

      |
      v

Stored in String Pool

      |
      v

References Shared


Advantages of String Pool

  • Memory optimization
  • Faster String reuse
  • Reduced garbage collection
  • Improved performance

Disadvantages of String Pool

  • Pool memory grows with many unique Strings
  • intern() excessive use may increase memory usage

Common Interview Mistake

Many developers think:

new String("Java")

uses existing pooled object only.

Actually:

  • new always creates new heap object.

Another Common Mistake

Many developers think:

==

compares String content.

Actually:

  • == compares references.
  • equals() compares content.

Best Practices

  • Prefer String literals over new String()
  • Use equals() for content comparison
  • Use intern() carefully
  • Avoid unnecessary String duplication

Realtime Enterprise Example

HTTP Status Values

String status = "SUCCESS";

Shared status Strings save memory across millions of requests.


Related Learning Topics


Professional Interview Answer

String Pool in Java is a special memory area inside the heap used to store String literals uniquely. When a String literal is created, JVM checks the pool first; if the String already exists, the existing reference is reused instead of creating a new object. This improves memory optimization and application performance by avoiding duplicate String objects. The String Pool relies on String immutability to safely share objects between references. Methods like intern() can explicitly place Strings into the pool. Enterprise systems, Spring Boot applications, banking platforms, and cloud-native microservices heavily benefit from String Pool optimization because Strings are widely used for configurations, API routes, statuses, security tokens, and identifiers.


Frequently Asked Questions

What is String Pool in Java?

String Pool is a special memory area used to store reusable String literals.

Why does Java use String Pool?

To save memory and improve performance by avoiding duplicate String objects.

Where is String Pool stored?

String Pool is stored inside heap memory.

What does intern() method do?

intern() returns the pooled version of a String.

Why does String Pool require immutability?

Because shared pooled objects must remain unchanged for safe reuse.

Why this Java question is important?

This interview question helps candidates understand real-time backend development concepts, practical problem solving, coding fundamentals, system design basics and production-ready application behavior.

Practice this question carefully for Java backend roles, Spring Boot developer interviews, microservices interviews, company interviews and full-stack developer preparation.

About the Author

Naresh Kumar is a Senior Java Backend Engineer with experience building enterprise applications using Java, Spring Boot, Microservices, Docker, Kubernetes and Cloud technologies.