String vs StringBuffer vs StringBuilder in Java Guide

When working with text data in Java, there are three commonly used classes to handle strings:

  1. String
  2. StringBuffer
  3. 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

FeatureStringStringBufferStringBuilder
MutabilityImmutableMutableMutable
Thread SafetyNot applicableThread-safe (synchronized)Not thread-safe
PerformanceSlow for modificationsSlower than StringBuilderFast (better performance)
Use CaseStatic text or rarely changedMulti-threaded contextSingle-threaded context
Memory UsageHigh (creates new objects)ModerateModerate

βœ… 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

Java developer with 9+ years of IT experience, sharing tutorials and tips to help learners master Java programming.

Leave a Reply

Your email address will not be published. Required fields are marked *