We often come across String related operations and for those kind of operations we rely mostly on either String or StringBuffer or StringBuilder classes. Today we will try to understand the difference between them.
The basic difference comes down to mutability, but there are some other subtle differences too.
- String: String class, as we all know is immutable. The immutability means that ones the String object is created, it cannot be changed. The object is stored in a constant string pool. As all the immutable classes in Java are thread safe, so is String class too. Thread safety means, that any two or more threads cannot access the object at the same time. Once an object is created of String class it cannot be changed,
String stringA = “Hello World”;
// a new object is created above.
// let us try to assign it a new value
stringA = “Good Night”;
// this means that the variable stringA is referencing to a new object and the value of that is “Good Night”, “Hello World” value still exists but it is not referenced by stringA variable, but not overridden.
- StringBuffer: This class is mutable and thread safe. The object created by this class is stored in a heap. Both StringBuffer and StringBuilder classes share common methods. The point to remember is each method in StringBuffer class is thread safe. Due to the implementation of thread safety of StringBuffer class, its performance is compromised. StringBuffer’s value can be changed as it is mutable and a value could be converted to normal String class using toString() method.
StringBuffer stringBuffer = new StringBuffer(“hello”);
// new StringBuffer object created
// now let us change the value of stringBuffer variable.
stringBuffer = new StringBuffer(“world”);
- StringBuilder: This class is mutable as well and has the same methods just as StringBuffer class, but StringBuilder is not thread safe and each of the methods used in StringBuilder as not thread safe either. Due to lack of thread safety mechanism, StringBuilder is much faster.
// implementation of StringBuilder
StringBuilder stringBuilder = new StringBuilder(“hello”);
// let us change the value of variable stringBuilder
stringBuilder = new StringBuilder(“world”);
// the value of variable stringBuilder is “world” now.
There are few other pointers for technical details of the above classes:
- StringBuilder and StringBuffer both uses Array as a backing object and that Array has to be re-allocated every time it gets full. So, every time append() method is called the array has to be resized.
- Consider this code,
String string = “Hello” + “World”;
This uses StringBuilder behind the scenes. So, for simple cases using StringBuilder will not make any sense, but if planning to build String object that are large enough (may be less than 4k) then using StringBuilder instead of concatenation is a better and efficient solution.
- If you are planning to use heavy implementations of String and String manipulations, than there are some better alternatives such as Ropes data structure.
Another alternative could be to extend ArrayList<String> class and then keeping track of each character lengths with each append() method and other mutation operations of list, and then overriding toString() method to create a StringBuilder object of exact size you need and loop through the list and build the output.