StringBuffer vs StringBuilder
Java is a very popular object oriented language. In Java, the String class is provided to hold a sequence of characters that cannot be modified (once initialized). Alternatively, Java programming language provides two types of mutable sequences of characters. That is, when the programmers need to modify a certain String (after initialization), they need to use the StringBuffer class or the StringBuilder class, instead of the String class. StringBuffer was introduced in JDK 1.0 and StringBuilder class was introduced in JDK 1.5, actually as a replacement for StringBuffer class (for single-thread environments).
What is StringBuffer?
StringBuffer class was introduced in JDK 1.0. StringBuffer class belongs to the java.lang package and is inherited from the generic java.lang.object. Programmers cannot extend it further since It is a final class. StringBuffer class implements Serializable, Appendable and CharSequience interfaces. An object of the class StringBuffer can hold a sequence of characters that is mutable and thread-safe. That means, it is very much similar to a String object, but the sequence of characters (length and content) can be changed at any time after initializing the StringBuffer object. However, that should be done using the specific methods provided by the StringBuffer class. There are two principle operations in StringBuffer class. They are provided by append() and insert() methods. These methods are overloaded, so they are able to accept data of any type such as integer and long. Both methods firstly transform any input to a string, and then adds (appends or inserts) the characters of the corresponding string to the existing Stribbuffer object. The append() method adds the converted string to the end of the existing StringBuffer object, while insert() method will add the input characters to the specified insertion point.
What is StringBuilder?
StringBuilder class was introduced in JDK 1.5. StringBuilder API is very much similar to StringBuffer API. In fact, StringBuilder class was actually introduced as a replacement for the StringBuffer class (for single-thread applications). StringBuilder class belongs to the java.lang package and is inherited from the generic java.lang.object. It is a final class and so the programmers cannot extend it. StringBuilder class implements Serializable, Appendable and CharSequience interfaces. An object of the class StringBuilder can hold a sequence of characters that is mutable but not thread-safe. That means, it is very much similar to a String object, but the string can be changed at any time. But StringBuilder class does not provide synchronization, and therefore is claimed to be faster than using StringBuffer class. StringBuilder class provides append() and insert() methods with exactly similar functionality as in StringBuffer class.
What is the difference between StringBuffer and StringBuilder?
Although, StringBuilder and StringBuffer classes can be used for mutable sequences of characters in Java, they have a key difference. Unlike StringBuffer class, StringBuilder class is not thread-safe, and provides no synchronization. Therefore, it is recommended that the StringBuilder class should be used in place of StringBuffer class in single-thread applications, because it is claimed that StringBuilder class will be much faster than StringBuffer class (under normal circumstances).