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 and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- String vs StringBuilder vs StringBuffer in Java
- Sorting collection of String and StringBuffer in Java
- A Java Random and StringBuffer Puzzle
- equals() on String and StringBuffer objects in Java
- StringBuffer class in Java
- StringBuffer insert() in Java
- StringBuffer deleteCharAt() Method in Java with Examples
- StringBuffer appendCodePoint() Method in Java with Examples
- StringBuffer delete() Method in Java with Examples
- StringBuffer replace() Method in Java with Examples
- StringBuffer reverse() Method in Java with Examples
- StringBuffer append() Method in Java with Examples
- StringBuffer setLength() in Java with Examples
- StringBuffer subSequence() in Java with Examples
- StringBuffer codePointCount() method in Java with Examples
- StringBuffer codePointBefore() method in Java with Examples
- StringBuffer trimToSize() method in Java with Examples
- StringBuffer toString() method in Java with Examples
- StringBuffer codePointAt() method in Java with Examples
- StringBuffer ensureCapacity() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.