How to Use Stringbuffer Instead of String Concatenation in java ?

Use Stringbuffer Instead of String Concatenation

The String class is the most commonly used class in Java. Especially in Web applications, it is used extensively to generate and format HTML content.
String is designed to be immutable; in order to modify a String, you have to create a new String object. 

Therefore, string concatenation can result in creating many intermediate String objects before the final String can be constructed. StringBuffer is the mutable companion class of String; it allows you to modify the String. Therefore, StringBuffer is generally more efficient than String when concatenation is needed.


This section also features the following practices:
  1. Use StringBuffer Instead of String Concatenation If You Repeatedly Append to a String In Multiple Statements
  2. Use Either String or StringBuffer If the Concatenation Is Within One Statement
  3. Use StringBuffer Instead of String Concatenation If You Know the Size of the String


Comments