Well, the most important difference between String and StringBuffer/StringBuilder in java is that String object is immutable whereas StringBuffer/StringBuilder objects are mutable.
By immutable, we mean that the value stored in the String object cannot be changed. Then the next question that comes to our mind is “If String is immutable then how am I able to change the contents of the object whenever I wish to?” . Well, to be precise it’s not the same String object that reflects the changes you do. Internally a new String object is created to do the changes.
So suppose you declare a String object:
String myString = “My example”;
Now, you want to append “string” to the same String. What do you do?
myString = myString + ”String”;
What is really happening hear is that we are NOT updating the existing string object. we are reallocating new memory all together, copying the “My example” data and appending “String” to the end, then settings the myString reference to point to this new memory.
So it follows that this “copy + paste and move stuff around in memory” process can be very expensive if done repitively especially recursively as it would create lot of string objects in memory.
So how to overcome these performance issue? We have two option
StringBuffer and StringBuilder.
Well, since StringBuffer/StringBuilder objects are mutable, we can make changes to the value stored in the object. What this effectively means is that string operations such as append would be more efficient if performed using StringBuffer/StringBuilder objects than String objects.
whats the difference between StringBuffer and StringBuilder?
StringBuffer and StringBuilder have the same methods with one difference and that’s of synchronization. StringBuffer is synchronized( which means it is thread safe and hence you can use it when you implement threads for your methods) whereas StringBuilder is not synchronized( which implies it isn’t thread safe). So, if you aren’t going to use threading then use the StringBuilder class as it’ll be more efficient than StringBuffer due to the absence of synchronization.
In short; A
StringBuffer or its non-synchronized varient StringBuilder are mutable
, They are used when you need to construct a string piece by piece without the performance overhead of constructing lots of little
Strings along the way.
The maximum length for both is Integer.MAX_VALUE, because they are stored internally as arrays(called as String Buffer), and Java arrays only have an
int for their length pseudo-field. . As long as the length of the character sequence contained in the string buffer does not exceed the capacity, it is not necessary to allocate a new internal buffer array. If the internal buffer overflows, it is automatically made larger.
The performance improvement between String and StringBuffer
for multiple concatenation is quite significant. A StringBuffer is used to create a single string from many strings, e.g. when you want to append parts of a String in a loop.
You should use a StringBuilder instead of a StringBuffer when you have only a single Thread accessing the StringBuffer, since the StringBuilder is not synchronized and thus faster