MediumExperience level: 3-5 years · 8-15 years1 min read

How does String interning work, and when would you actually use intern() in production?

Reviewed by Gurusankar M.

Asked inAmazonGoogleDeloitte
#string#intern#string pool#memory#deduplication
Report issue

Short Answer

intern() returns the pooled canonical instance for an equal String, so duplicates share one object. Useful when ingesting huge volumes of repeated Strings (e.g. country codes) to cut heap — but misuse can bloat the pool and hurt GC.

On this page

Concept Made Simple

Coffee Chat Question

“How does String interning work, and when would you actually use intern() in production?”

Remember It Faster

Mind Map Answer

The String pool is a JVM-managed set of canonical Strings. intern() says *'give me the shared copy of this value'*. Two interned equal Strings are ==.

Before intern
1M duplicate "IN" objects
After intern
1 shared "IN" object
Risk
interning unique values bloats the pool

Learn by Doing

Hands-on Keyboard

java
String a = new String("IN").intern();
String b = "IN";
System.out.println(a == b); // true — both pooled
Output
true

Think Beyond the Expected

What If?

You intern() every value from a 50M-row feed and GC time spikes — why?

Interning unique/high-cardinality values fills the pool, which lives on the heap and is GC-scanned. Only intern low-cardinality, frequently-repeated values; otherwise you trade duplicate Strings for a giant pool.

Real World

A market-data pipeline interned a small set of exchange/currency codes appearing billions of times, cutting heap noticeably — but the team explicitly avoided interning order IDs (all unique).

Interviewer's Expectation

Keywords they're listening for:

  • canonical pooled instance
  • low-cardinality dedup
  • pool lives on heap (Java 7+)
  • GC/bloat risk

Common Mistakes

  • Interning high-cardinality values
  • Assuming intern() is free
  • Using intern() for locking (anti-pattern)

Best Practices

  • Intern only low-cardinality, high-frequency Strings
  • Prefer G1 string deduplication over manual intern() for general dedup
  • Measure heap before/after with a profiler

Follow-up Questions

  1. Where did the pool move in Java 7 and why?
  2. What is JVM string deduplication (-XX:+UseStringDeduplication) with G1?
  3. How does this relate to flyweight pattern?

Continue Learning with AI

Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.

Plain-language foundations

I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner.

Topic: Strings (Core Java)
Interview question: "How does String interning work, and when would you actually use intern() in production?"

Please:
1. Explain the core idea in simple, plain language, using an everyday analogy.
2. Define any technical terms you use.
3. Walk through one small, concrete example.
4. Finish with a single sentence I can easily remember.

Keep the tone friendly and assume I'm new to this topic.
Open inChatGPTGeminiClaude

Was this answer helpful?

Affiliate links

Equipment for a comfortable, productive setup.

As an Amazon Associate I earn from qualifying purchases.