Why StringBuffer is mutable in Java?

We all know that the String class in Java is mutable i.e. once we create a String variable we cannot modify its data or do any manipulations.

But, there may be scenarios where we need to modify the data of String variables. In such cases, we could use StringBuffer class.

This class −

  • is like a String, but can be modified.
  •  It contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls.
  • Is safe for use by multiple threads.