Skip to main content
1:1 mock interviews with Sumit, scored on a real hiring rubric.Book a slot →
TOPIC 3 OF 6 · 2 MIN READ

String Immutability, the String Pool and StringBuilder

Strings in Java are immutable and literals are stored in the string pool. Questions about == vs equals, why String is immutable and when to use StringBuilder appear in nearly every Java interview.

What you will learn

  • Explain why String is immutable
  • Compare == and equals for strings
  • Explain the string pool and intern()
  • Choose String, StringBuilder or StringBuffer

Key concepts

Immutability

Once created, a String cannot change; methods like toUpperCase return a new String. This makes strings safe to share between threads, safe as HashMap keys (hash is cached), and safe for security-sensitive values like class names and URLs.

String pool

A heap area where the JVM keeps one copy of each string literal. "abc" in two places refers to the same object. new String("abc") always creates a new object.

== vs equals

== compares references (same object). equals compares characters. Always use equals for content.

StringBuilder

A mutable character buffer for building strings in loops. StringBuffer is the older synchronized version; StringBuilder is preferred.

Step by step

Predict the output

String a = "java";
String b = "java";
String c = new String("java");
String d = c.intern();

System.out.println(a == b);       // true  (same pooled literal)
System.out.println(a == c);       // false (new object on the heap)
System.out.println(a.equals(c));  // true  (same characters)
System.out.println(a == d);       // true  (intern returns the pooled one)

String e = "ja" + "va";           // compile-time constant → pooled
System.out.println(a == e);       // true

Examples

Building strings in a loop

// Slow: creates a new String every iteration
String s = "";
for (int i = 0; i < 10_000; i++) s += i;

// Fast
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 10_000; i++) sb.append(i);
String result = sb.toString();

// Joining a collection
String csv = String.join(",", names);

Practise it

Try these without looking back. If you can explain each one out loud, you are ready for this question.

  • Write a method that reverses words in a sentence using StringBuilder.
  • Explain why passwords are often stored in char[] rather than String.

Interview questions

What interviewers ask next on this topic. Answer out loud first, then open the model answer.

How many objects does new String("hello") create?

Up to two: the literal "hello" in the pool if it is not already there, and a new String object on the heap. If the literal already exists, only one new object is created.

Why are strings good HashMap keys?

They are immutable, so their hashCode never changes after insertion, and String caches its hash. A mutable key that changes after insertion ends up in the wrong bucket and cannot be found.

Summary

Strings are immutable and literals are pooled; compare content with equals, not ==, and use StringBuilder when building strings in loops.

Stuck on this one? Talk to Sumit.

Keys: F focus mode · ← → previous / next topic · Esc leave focus