CodeToConcept
About

java

java Tutorials

Introduction
Java Variables
Java Data Types
Java Type Casting
Java Operators
Java Input/Output
Java Class & Object
java Method
Java Constructor
Java this keyword
Java super keyword
Java Method Overloading and Overriding
Java Interface
Java Abstract Class
Java Memory Management
Design Patterns
JDBC (Database Connectivity)
Java IDEs

java Tutorials

Introduction
Java Variables
Java Data Types
Java Type Casting
Java Operators
Java Input/Output
Java Class & Object
java Method
Java Constructor
Java this keyword
Java super keyword
Java Method Overloading and Overriding
Java Interface
Java Abstract Class
Java Memory Management
Design Patterns
JDBC (Database Connectivity)
Java IDEs

Sample Question

Think it through, then check the tutorial.

CodeToConcept

Quickly learn through simple, structured, and practical coding tutorials. Build your confidence from step zero to real-world experience.

© 2026 CodeToConcept. All rights reserved.

Quick Links

AboutTutorials

Tutorials

JavaHTMLCSS

Support

Privacy PolicyTerms of ServiceContact
GitHubTwitterYouTube

Immutable vs Mutable Strings in Java

Two variables can share a String without either variable being able to edit that object's text. A StringBuilder or StringBuffer is different: an append or delete changes its current contents. The choice matters when text is shared or built through many steps.

What Does Immutable Mean?

Immutable means that an object's state cannot be changed after the object has been created.

A Java String is immutable.

For example:

String text = "Hello";

Once the String object containing "Hello" is created, its contents cannot be changed.

If we write:

text = text + " Java";

Java creates a new String representing:

Hello Java

The original "Hello" object remains unchanged.

Example of String Immutability

String text = "Hello";
 
text.concat(" Java");
 
System.out.println(text);

Output

Hello

Why? Because concat() returns a new String.

It does not modify the original object.

Correct usage:

String text = "Hello";
 
text = text.concat(" Java");
 
System.out.println(text);

Output

Hello Java

What Does Mutable Mean?

Mutable means that an object's state can be changed after the object has been created.

StringBuilder is mutable.

StringBuilder builder = new StringBuilder("Hello");
 
builder.append(" Java");
 
System.out.println(builder);

Output

Hello Java

The same StringBuilder object is modified.

String vs StringBuilder

String

String text = "Hello";
 
text = text + " Java";

A new String is created for the modified value.

StringBuilder

StringBuilder builder = new StringBuilder("Hello");
 
builder.append(" Java");

The existing StringBuilder object is modified.

Why is String Immutable?

String immutability provides several useful properties.

1. String Pool Sharing

String literals can be shared through the String pool.

String a = "Java";
String b = "Java";

Both variables may refer to the same pooled String object.

Because Strings are immutable, one reference cannot change the contents for the other reference.

2. Safe Sharing

An immutable object can be shared between different parts of a program without worrying that another part will modify its contents.

3. Stable Hash Code

Strings are frequently used as keys in hash-based collections such as HashMap.

Because the String content cannot change, its hash-based identity remains stable after creation.

Example

String key = "username";
 
java.util.Map<String, Integer> map = new java.util.HashMap<>();
 
map.put(key, 100);

If the String key were mutable, changing its contents after insertion could cause problems when locating the entry.

4. Thread-Friendly Sharing

Immutable objects are naturally easier to share between threads because their state cannot be changed.

Mutable Classes

Java provides mutable alternatives:

String
  ↓
Immutable
 
StringBuilder
  ↓
Mutable
 
StringBuffer
  ↓
Mutable

When Should You Use String?

Use String when the text does not need repeated modification.

For example:

String name = "John";
String country = "India";
String email = "john@example.com";

These values are often naturally represented as Strings.

When Should You Use StringBuilder?

Use StringBuilder when you need to repeatedly modify or build text.

Example

StringBuilder result = new StringBuilder();
 
for (int i = 1; i <= 5; i++) {
    result.append(i);
}

This avoids repeatedly creating intermediate immutable String objects.

When Should You Use StringBuffer?

Use StringBuffer when synchronized mutable string operations are specifically required.

StringBuffer buffer = new StringBuffer();
 
buffer.append("Hello");
buffer.append(" Java");

Comparison

FeatureStringStringBuilderStringBuffer
MutableNoYesYes
Can modify existing objectNoYesYes
Thread-safe by synchronizationImmutable, so state cannot be changedNoYes
Good for repeated modificationsUsually not idealYesYes
Typical useGeneral textBuilding/modifying textSynchronized mutable text

Simple Real-World Example Imagine writing on a printed document. String Think of a String as a printed label. Once printed, the text on that label does not change. If you need different text, you create another label. StringBuilder Think of StringBuilder as an editable document. You can add, remove, or replace text in the same document.

Key Takeaway

String is immutable, meaning its contents cannot be changed after creation. StringBuilder and StringBuffer are mutable, meaning their contents can be modified after creation. Use String for normal text and mutable classes when frequent modifications are required.

See you in next page....🙂

← String MethodsHow many ways to create String in Java →
← String MethodsHow many ways to create String in Java →