javastringoopstringbuilder

String, StringBuffer, and StringBuilder


What is some real time situation to compare String, StringBuffer, and StringBuilder?


Solution

  • Mutability Difference:

    String is immutable. If you try to alter their values, another object gets created, whereas StringBuffer and StringBuilder are mutable, so they can change their values.

    Thread-Safety Difference:

    The difference between StringBuffer and StringBuilder is that StringBuffer is threadsafe. So when the application needs to be run only in a single thread, then it is better to use StringBuilder. StringBuilder is more efficient than StringBuffer.

    Situations: