Lesson 1 of 616 minModule progress 0%

Module 10: Strings, Files, and Everyday Core APIs

Strings, Immutability, and the String Pool

Understand why Strings are immutable, how that affects performance, and when the string pool matters.

Author

Java Learner Editorial Team

Reviewer

Technical review by Java Learner

Last reviewed

2026-04-17

Java version

Java 25 LTS

How this lesson was prepared: AI-assisted draft, manually edited for clarity, and checked against current Java documentation and runnable examples.

Learning goals

  • Explain why Java strings are immutable
  • Recognize when repeated concatenation becomes wasteful
  • Choose string tools more intentionally

Strings are immutable: Once a String is created, its content cannot change. New operations produce new string objects instead.

That design has benefits: Strings become safer to share, easier to cache, and reliable as map keys or security-sensitive values.

But there is a cost: Repeated concatenation in loops creates extra objects and can become inefficient.

Intermediate habit: Use plain strings for fixed text, but switch tools when you are building text repeatedly.

Runnable examples

Concatenation creates a new string

String text = "Java";
text = text + " Learner";
System.out.println(text);

Expected output

Java Learner

Mini exercise

Write a short note using string concatenation, then explain why the original string value did not change in place.

Summary

  • Strings are immutable by design.
  • That helps safety and predictability.
  • Heavy repeated building should use a better tool than raw concatenation.

Next step

Next, use `StringBuilder` when text assembly becomes repetitive or loop-driven.

Sources used

Advertisement

Lesson check

What does string immutability mean?

Next lesson →