Difference Between StringBuffer and StringBuilder in Java
Strings in Java are the objects that are backed internally by a char array. Since arrays are immutable(cannot grow), Strings are immutable as well. Whenever a change to a String is made, an entirely new String is created. However, java provides multiple classes through which strings can be used. Two such classes are StringBuffer and StringBuilder. In this article, we will see the difference between both the classes.
StringBuffer Class: StringBuffer is a peer class of String that provides much of the functionality of strings. The string represents fixed-length, immutable character sequences while StringBuffer represents growable and writable character sequences. StringBuffer may have characters and substrings inserted in the middle or appended to the end. It will automatically grow to make room for such additions and often has more characters preallocated than are actually needed, to allow room for growth. In order to create a string buffer, an object needs to be created, (i.e.), if we wish to create a new string buffer with name str, then:
StringBuffer str = new StringBuffer();
Example: The following is an example which implements the StringBuffer class.
StringBuilder Class: Similar to StringBuffer, the StringBuilder in Java represents a mutable sequence of characters. Since the String Class in Java creates an immutable sequence of characters, the StringBuilder class provides an alternative to String Class, as it creates a mutable sequence of characters. The function of StringBuilder is very much similar to the StringBuffer class, as both of them provide an alternative to String Class by making a mutable sequence of characters. Similar to StringBuffer, in order to create a new string with the name str, we need to create an object of StringBuilder, (i.e.):
StringBuilder str = new StringBuilder();
Example: The following is an example which implements the StringBuilder class.
Conversion from StringBuffer to StringBuilder
The StringBuffer cannot be directly converted to StringBuilder. We first need to convert the StringBuffer to a String object by using the inbuilt method toString(). After converting it to a string object, we can simply create a StringBuilder using the constructor of the class. For example:
Conversion from StringBuilder to StringBuffer
Similar to the above conversion, the StringBuilder cannot be converted to the StringBuffer directly. We first need to convert the StringBuilder to the String object by using the inbuilt method toString(). Now, we can create a StringBuilder using the constructor. For example:
StringBuilder vs StringBuffer in Java
|StringBuffer Class||StringBuilder Class|
|StringBuffer is present in Java.||StringBuilder was introduced in Java 5.|
|StringBuffer is synchronized. This means that multiple threads cannot call the methods of StringBuffer simultaneously.||StringBuilder is asynchronized. This means that multiple threads can call the methods of StringBuilder simultaneously.|
|Due to synchronization, StringBuffer is called a thread safe class.||Due to its asynchronous nature, StringBuilder is not a thread safe class.|
|Due to synchronization, StringBuffer is lot slower than StringBuilder.||Since there is no preliminary check for multiple threads, StringBuilder is a lot faster than StringBuffer.|
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.