When working with text data in Java, there are three commonly used classes to handle strings:
- String
- StringBuffer
- StringBuilder
Each of these serves a different purpose and has its own advantages and limitations. Understanding when and how to use them is key for writing efficient Java programs.
β 1. String
- Immutable Object: Once a
String
object is created, its value cannot be changed. - Every modification creates a new
String
object in memory. - Useful when the string content doesn’t change often.
π§ Example:
public class StringExample {
public static void main(String[] args) {
String text = "Java Knowledge Base";
System.out.println("Original String: " + text);
// Concatenation creates a new String object
text = text + " - Learn Java Effectively";
System.out.println("Modified String: " + text);
}
}
β Output:
Original String: Java Knowledge Base
Modified String: Java Knowledge Base - Learn Java Effectively
β‘ Key Point: Inefficient for many modifications due to creation of new objects and higher memory consumption.
β 2. StringBuffer
- Mutable Class: Allows modification of the string content without creating new objects.
- Thread-Safe: All methods are synchronized.
- Suitable when thread safety is required.
π§ Example:
public class StringBufferExample {
public static void main(String[] args) {
StringBuffer textBuffer = new StringBuffer("Java Knowledge Base");
System.out.println("Original StringBuffer: " + textBuffer);
// Append text
textBuffer.append(" - Learn Java Effectively");
System.out.println("Modified StringBuffer: " + textBuffer);
// Insert text
textBuffer.insert(5, " Awesome");
System.out.println("After Insert: " + textBuffer);
}
}
β Output:
Original StringBuffer: Java Knowledge Base
Modified StringBuffer: Java Knowledge Base - Learn Java Effectively
After Insert: Java Awesome Knowledge Base - Learn Java Effectively
β‘ Key Point: Suitable for multi-threaded environments but slightly slower than StringBuilder
.
β 3. StringBuilder
- Mutable Class: Like
StringBuffer
, but not synchronized (not thread-safe). - Faster than
StringBuffer
due to the lack of synchronization. - Recommended when working in a single-threaded context.
π§ Example:
public class StringBuilderExample {
public static void main(String[] args) {
StringBuilder textBuilder = new StringBuilder("Java Knowledge Base");
System.out.println("Original StringBuilder: " + textBuilder);
// Append text
textBuilder.append(" - Learn Java Effectively");
System.out.println("Modified StringBuilder: " + textBuilder);
// Insert text
textBuilder.insert(5, " Awesome");
System.out.println("After Insert: " + textBuilder);
}
}
β Output:
Original StringBuilder: Java Knowledge Base
Modified StringBuilder: Java Knowledge Base - Learn Java Effectively
After Insert: Java Awesome Knowledge Base - Learn Java Effectively
β‘ Key Point: Best choice when working in a single-threaded environment and performance is critical.
βοΈ Comparison Table
Feature | String | StringBuffer | StringBuilder |
---|---|---|---|
Mutability | Immutable | Mutable | Mutable |
Thread Safety | Not applicable | Thread-safe (synchronized) | Not thread-safe |
Performance | Slow for modifications | Slower than StringBuilder | Fast (better performance) |
Use Case | Static text or rarely changed | Multi-threaded context | Single-threaded context |
Memory Usage | High (creates new objects) | Moderate | Moderate |
β Conclusion
- Use String when the string content does not change.
- Use StringBuffer when you need thread safety.
- Use StringBuilder for efficient single-threaded string manipulation