Lesson 5: Strings & the String Pool
06 Jul 2026 1 min Swarnil Singhai
Strings are immutable in Java — every 'modification' creates a new object, and literal strings are cached in a pool to save memory.
Real-time example
A logging library builds thousands of log lines per second; knowing Strings are immutable explains why StringBuilder exists for that hot path.
String a = "order-42";
String b = "order-42";
System.out.println(a == b); // true - same pooled literal
StringBuilder log = new StringBuilder();
for (int i = 0; i < 3; i++) log.append("event-").append(i).append(" ");
System.out.println(log);
What's happening
== on pooled literals can be true, but never rely on it for equals — always use .equals() for string comparison.
Use StringBuilder in loops; using + on Strings in a loop silently creates a new object every single iteration.
Advertisement