StringBuilder and StringBuffer – A way to create mutable Strings in Java
|String– One of the most important class in Java Programming have a very important concept of Immutability i.e. once created we can not change the value of that object.
So what happens when you perform any operation like toUpperCase(), sub-string operations, string modification operations and other operations on Java Strings?
Whenever you perform such an operation on Strings a new object is created in the memory and you use the newly created object and the previous object remains in the memory until garbage collector cleans it from the memory.
Read more – Why Strings in Java are Mutable or Final?
Since String class is a widely used class a lot of string objects are created in the memory as our program runs.
So, don’t you think it is a performance issue?
And the answer is, YES.
Although JVM runs the Garbage collector and it keeps on cleaning the memory by removing the objects from the memory as soon as it finds them unusable or un-referenced. But Java has the concept of String Pool and the objects from the pool are not removed instantly but it took some time.
So to introduce a new feature and to create Mutable Strings Java has two classes-
In one line Difference between these two classes is that StringBuffer is thread safe whereas StringBuilder is not and StringBuilder is faster than StringBuffer.
So lets dig a little deep into this.
StringBuffer Class-
This is an older class that is used to make mutable Strings in java. Whenever we do some change in any String made by using this class then no new object is created because the changes are made in the same object.
This class is thread safe i.e. it can work properly in multiple Thread environment. The methods of this class are synchronised so that whenever multiple Threads invoke some methods on the object then they execute in the order of the method calls made by each of the individual Thread.
Main Methods are:-
- Append – Appends data at the end of String.
Example – bufferReference.append(data); - Insert – Inserts the data at any given Index.
Example – bufferReference.insert( int index, data);
Both of these methods are overloaded properly so that they can accept any type of data and append or insert them into the existing string object. Rest of the methods are to perform some basic String operations like getting subString, getting char at a index, toString(), trimming etc.
package codingeekStringTutorials; public class StringBufferJava { public static void main(String[] args) { StringBuffer buffer = new StringBuffer("Codingeek"); System.out.println("name = "+buffer.toString() +", hashcode = " + buffer.hashCode()); // append Example of StringBuffer buffer.append(" - A Programmers Home"); System.out.println("name = "+buffer.toString() +", hashcode = " + buffer.hashCode()); // Insert Example of StringBuffer buffer.insert(9, ".com"); System.out.println("name = "+buffer.toString() +", hashcode = " + buffer.hashCode()); } }
Output:-
name = Codingeek, hashcode = 16585653
name = Codingeek - A Programmers Home, hashcode = 16585653
name = Codingeek.com - A Programmers Home, hashcode = 16585653
In the above example the same value of Hascode every time shows that we are working on the same object every time i.e. no new object was created during any of the operations and the same explanation exists for StringBuilder.
StringBuilder Class –
This was a new but smaller version of StringBuffer class and was introduced with the release of JDK 5. It is similar to StringBuffer but it does not provide any guarantee for thread safety or synchronisation.
It is used at the places where there is only a single Threaded application as it is much faster in most of the cases( think yourself-> No synchronisation-> Less Overhead-> Improved performance)
Rest of it is similar to StringBuffer with same methods and declarations. We just have to use diffetent class during initialisation and the rest of the code is need not to be changed as it also have same methods.
package codingeekStringTutorials; public class StringBuilderJava { public static void main(String[] args) { StringBuilder builder = new StringBuilder("Codingeek"); System.out.println("name = "+builder.toString() +", hashcode = " + builder.hashCode()); // append Example of StringBuilder builder.append(" - A Programmers Home"); System.out.println("name = "+builder.toString() +", hashcode = " + builder.hashCode()); // Insert Example of StringBuilder builder.insert(9, ".com"); System.out.println("name = "+builder.toString() +", hashcode = " + builder.hashCode()); } }
Output:-
name = Codingeek, hashcode = 16585653
name = Codingeek - A Programmers Home, hashcode = 16585653
name = Codingeek.com - A Programmers Home, hashcode = 16585653