Difference between String, Stringbuffer and StringBuilder

In Java, String, StringBuffer, and StringBuilder are classes used to work with sequences of characters. While they might seem similar, they have important differences in terms of mutability, thread safety, and performance.

1. String

  • Immutability: Once a String object is created, it cannot be modified. Any operation that seems to modify the string (like concatenation) actually creates a new String object.
  • Performance: Since String is immutable, operations that modify strings, like concatenation in a loop, can be slow and memory-inefficient because they create multiple intermediate objects.
  • Thread Safety: String is inherently thread-safe because it is immutable.
  • Usage: String is used when the sequence of characters is not expected to change frequently.

java String str = "Hello"; str = str + " World"; // Creates a new String "Hello World"

2. StringBuffer

  • Mutability: StringBuffer objects can be modified after creation. Unlike String, when you modify a StringBuffer object, it changes the same object instead of creating a new one.
  • Thread Safety: StringBuffer is synchronized, meaning it is thread-safe. Multiple threads can access a StringBuffer object safely without causing data corruption.
  • Performance: Due to synchronization, StringBuffer can be slower than StringBuilder when thread safety is not required.
  • Usage: StringBuffer is used in scenarios where the string content is expected to change frequently and thread safety is a concern.

java StringBuffer sb = new StringBuffer("Hello"); sb.append(" World"); // Modifies the same StringBuffer object

3. StringBuilder

  • Mutability: Like StringBuffer, StringBuilder objects are mutable, meaning they can be modified without creating new objects.
  • Thread Safety: StringBuilder is not synchronized, making it not thread-safe. However, this also means it is faster than StringBuffer in single-threaded environments.
  • Performance: StringBuilder is faster than StringBuffer due to the lack of synchronization overhead.
  • Usage: StringBuilder is preferred in situations where the string content changes frequently, and thread safety is not a concern, especially in single-threaded environments.

java StringBuilder sb = new StringBuilder("Hello"); sb.append(" World"); // Modifies the same StringBuilder object

Summary of Differences:

\| Feature \| String \| StringBuffer \| StringBuilder \|

\|---------------------\|---------------\|-----------------\|-----------------\|

\| Mutability \| Immutable \| Mutable \| Mutable \|

\| Thread Safety \| Thread-safe \| Thread-safe \| Not thread-safe \|

\| Performance \| Slower for modifications \| Slower (due to synchronization) \| Faster \|

\| Use Case \| Immutable sequences, low modification \| Modifiable sequences in a multi-threaded environment \| Modifiable sequences in a single-threaded environment \|

In conclusion, choose String when immutability and thread safety are important, StringBuffer when you need a mutable and thread-safe sequence of characters, and StringBuilder when you need a mutable sequence of characters in a single-threaded environment.