In Java, String, StringBuilder, and StringBuffer are used to store and manipulate sequences of characters. The main difference between them lies in mutability, thread safety, and performance. String is immutable, while StringBuilder and StringBuffer are mutable, making them suitable for different string manipulation requirements.
- String: Immutable and suitable for text that does not require frequent modifications.
- StringBuilder: Mutable and efficient for repeated string modifications without synchronization.
- StringBuffer: Mutable and synchronized, making individual method calls thread-safe.
String
A String in Java is 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 instead of changing the original object.
public class StringExample{
public static void main(String[] args){
String str = "Hello";
// creates a new object
str.concat(" World");
System.out.println(str);
}
}
Output
Hello
Explanation: Here, str.concat(" World") does not modify the original string. Instead, it creates a new String object, and the old one remains unchanged.
StringBuilder
StringBuilder is a mutable sequence of characters introduced in Java 5. Unlike String, it allows modifications to the existing object using methods such as append(), insert(), delete(), and reverse().
StringBuilder is not synchronized, making it generally suitable for repeated string modifications when external synchronization is not required.
public class Geeks{
public static void main(String[] args){
StringBuilder sb = new StringBuilder("Hello");
sb.append(" World");
System.out.println(sb);
}
}
Output
Hello World
Explanation: In the above example append() method modifies the existing object directly without creating a new one.
StringBuffer
StringBuffer is a mutable sequence of characters similar to StringBuilder. However, its main methods are synchronized, allowing individual operations to be performed safely when multiple threads access the same object. This synchronization introduces some overhead compared with StringBuilder.
public class Geeks{
public static void main(String[] args){
StringBuffer sb = new StringBuffer("Hello");
sb.append(" World");
System.out.println(sb);
}
}
Output
Hello World
Explanation: In the above example append() modifies the existing object safely in a multi-threaded environment.
Use Cases of String, StringBuilder, and StringBuffer
Geeks now you must be wondering when to use which one, do refer below as follows:
- If a string is going to remain constant throughout the program, then use the String class object because a String object is immutable.
- If a string can change (for example: lots of logic and operations in the construction of the string) and will only be accessed from a single thread, using a StringBuilder is good enough.
- If a string can change and will be accessed from multiple threads, use a StringBuffer because StringBuffer is synchronous, so you have thread-safety.
- If you don't want thread-safety than you can also go with StringBuilder class as it is not synchronized.
StringBuilder vs String vs StringBuffer
The table below demonstrates the difference between String, StringBuilder and StringBuffer:
Features | String | StringBuilder | StringBuffer |
|---|---|---|---|
Mutability | Strings are immutable(creates new objects on modification) | StringBuilder are mutable(modifies in place) | StringBuffer are mutable (modifies in place) |
Thread-Safe | It is thread-safe | It is not thread-safe | It is thread-safe |
Performance | It is slow because it creates an object each time | It is faster (no object creation) | it is slower due to synchronization overhead |
use Case | Fixed, unchanging strings | Single-threaded string manipulation | Multi-threaded string manipulation |