StringBuilder vs StringBuffer in Java

Introduction

In Java, strings are immutable, meaning once created, their values cannot be changed. To perform modifications like appending, inserting, or deleting characters efficiently, Java provides two classes: StringBuilder and StringBuffer. Both allow the creation of mutable (modifiable) strings, but they differ mainly in synchronization and performance.


Code: Example of StringBuilder and StringBuffer

public class StringBuilderVsStringBuffer {
public static void main(String[] args) {
// Using StringBuilder
StringBuilder sb = new StringBuilder("Hello");
sb.append(" World");
sb.insert(5, ",");
sb.replace(0, 5, "Hi");
sb.delete(3, 4);
System.out.println("StringBuilder result: " + sb);
// Using StringBuffer
StringBuffer sbf = new StringBuffer("Java");
sbf.append(" Programming");
sbf.insert(4, " Language");
sbf.replace(0, 4, "Core Java");
sbf.delete(5, 9);
System.out.println("StringBuffer result: " + sbf);
}
}

Explanation of Each Code Part

1. StringBuilder Example

StringBuilder sb = new StringBuilder("Hello");
  • Creates a StringBuilder object with the initial string "Hello".
  • Strings created using StringBuilder are mutable, meaning you can modify them.

sb.append(" World");
  • Adds " World" to the end of the existing string.
  • Result: "Hello World"

sb.insert(5, ",");
  • Inserts a comma at position 5.
  • Result: "Hello, World"

sb.replace(0, 5, "Hi");
  • Replaces characters from index 0 to 5 with "Hi".
  • Result: "Hi, World"

sb.delete(3, 4);
  • Deletes the character between index 3 and 4.
  • Result: "Hi World"

2. StringBuffer Example

StringBuffer sbf = new StringBuffer("Java");
  • Creates a StringBuffer object with the initial string "Java".
  • Like StringBuilder, it is mutable, but synchronized, making it thread-safe.

sbf.append(" Programming");
  • Adds " Programming" to the end.
  • Result: "Java Programming"

sbf.insert(4, " Language");
  • Inserts " Language" after "Java".
  • Result: "Java Language Programming"

sbf.replace(0, 4, "Core Java");
  • Replaces "Java" with "Core Java".
  • Result: "Core Java Language Programming"

sbf.delete(5, 9);
  • Deletes characters from index 5 to 9.
  • Result: "Core a Language Programming"

Comparison Table: StringBuilder vs StringBuffer

FeatureStringBuilderStringBuffer
Introduced InJava 5Java 1.0
SynchronizationNot synchronized (not thread-safe)Synchronized (thread-safe)
PerformanceFasterSlower due to synchronization
Use CaseSingle-threaded programsMulti-threaded programs
MutabilityMutableMutable
Packagejava.langjava.lang

Conclusion

  • Both StringBuilder and StringBuffer allow modification of strings without creating new objects.
  • StringBuilder is faster and should be used in single-threaded applications.
  • StringBuffer is thread-safe and should be used in multi-threaded environments.
  • Choosing between them depends on whether your program requires synchronization or higher performance.

Leave a Reply

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


Macro Nepal Helper