StringBuffer and StringBuilder In Java

What StringBuffer and StringBuilder In Java?

  • A String Builder is like a String, but can be modified. String has drawback that once created it can not be modified. To overcome that problem StringBuffer and Stringbuilder can be used.

  • Unlike Strings objects of type StringBuffer and Stringbuilder can be modified over and over again without leaving behind a lot of new unused objects.

  • he StringBuilder class was introduced as of Java 5 and the main difference between the StringBuffer and StringBuilder is that StringBuilders methods are not thread safe(not Synchronised).

  • It is recommended to use StringBuilder whenever possible because it is faster than StringBuffer. However if thread safety is necessary the best option is StringBuffer objects.



Difference between StringBuffer and StringBuilder

  • ‘StringBuffer’ are Thread Safe. The methods are synchronized. StringBuilder is not thread safe.

  • Where possible, it is recommended that String Builder should be used in preference to StringBuffer as it will be faster in most of the cases.


Important Methods of String Buffer:

  • append()

  • insert()

  • replace()

  • delete()

  • reverse()

  • replace()


Manikandan is Founder and author of codenread. Tech Blogger by Passion | Computer Science Engineer by Qualification