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 JavaThe original "Hello" object remains unchanged.
Example of String Immutability
String text = "Hello";
text.concat(" Java");
System.out.println(text);Output
HelloWhy? 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 JavaWhat 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 JavaThe 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
↓
MutableWhen 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
| Feature | String | StringBuilder | StringBuffer |
|---|---|---|---|
| Mutable | No | Yes | Yes |
| Can modify existing object | No | Yes | Yes |
| Thread-safe by synchronization | Immutable, so state cannot be changed | No | Yes |
| Good for repeated modifications | Usually not ideal | Yes | Yes |
| Typical use | General text | Building/modifying text | Synchronized 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....🙂