The String class in Java represents immutable character sequences. Once created, a String object cannot be modified. For example, String s = “Hello”;. Any operation like concatenation creates a new object.
StringBuffer and StringBuilder are used when you need mutable strings, meaning their content can be changed without creating new objects. StringBuffer is thread-safe (synchronized), while StringBuilder is faster but not synchronized.
Use StringBuffer in multithreaded environments. They both offer methods like .append(), .insert(), .reverse() etc.
Syntax:
sb.append(” there”);
These classes improve performance in scenarios involving heavy string manipulation like loops or file processing. Choose the right class based on your application’s thread-safety and performance needs.