Java provides String, StringBuilder, and StringBuffer classes to represent and manipulate sequences of characters. They differ primarily in mutability, thread safety, and performance.
- String objects are immutable, whereas StringBuilder and StringBuffer objects are mutable.
- StringBuffer provides synchronized operations, while StringBuilder does not.
1. String
String represents an immutable sequence of characters. Once a String object is created, its contents cannot be modified. Operations such as concat(), replace(), and substring() return a string result without modifying the original object.
public class Geeks {
public static void main(String[] args){
String str = "Hello";
// Concatenation does not modify the original string
str.concat(" World");
System.out.println(str);
}
}
Output
Hello
Explanation: The concat() method returns a new string containing "Hello World". Since the returned value is not assigned to str, the original string remains unchanged.
2. StringBuilder
StringBuilder represents a mutable sequence of characters introduced in Java 5. It supports modifications using methods such as append(), insert(), delete(), and reverse() without replacing the existing StringBuilder object.
public class Geeks{
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Hello");
// Modify the existing object
sb.append(" World");
System.out.println(sb);
}
}
Output
Hello World
Explanation: The append() method modifies the contents of the existing StringBuilder object. Since its methods are not synchronized, it is generally efficient for repeated string modifications within a single thread.
3. StringBuffer
StringBuffer represents a mutable sequence of characters with synchronized operations. It is suitable when multiple threads need to modify the same character sequence safely.
public class Geeks{
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("Hello");
// Modify the existing object
sb.append(" World");
System.out.println(sb);
}
}
Output
Hello World
Explanation: The append() method modifies the existing StringBuffer object. Its synchronization protects individual operations when multiple threads access the same object.
Difference Between String, StringBuilder and StringBuffer
Feature | String | StringBuilder | StringBuffer |
|---|---|---|---|
Mutability | Immutable | Mutable | Mutable |
Thread Safety | Safe to share due to immutability | Not inherently thread-safe | Individual operations are thread-safe |
Synchronization | Not required for immutable contents | Not synchronized | Provides synchronized operations |
Performance | Repeated modifications may be expensive | Generally faster for repeated modifications | May incur synchronization overhead |
Object Modification | Returns a string result without modifying the original object | Modifies the existing object | Modifies the existing object |
Introduced In | Java 1.0 | Java 5 | Java 1.0 |
When to Use String, StringBuilder and StringBuffer
- Use String for fixed text, configuration values, and occasional string operations.
- Use StringBuilder for constructing or repeatedly modifying strings when concurrent access is not required.
- Use StringBuffer when multiple threads need to modify the same character sequence using synchronized operations.
Note: StringBuffer provides thread safety for individual method calls. Operations involving multiple method calls may still require additional synchronization. StringBuilder and StringBuffer may also allocate new internal storage when their capacity increases.