Have you ever thought about the implications of this change in the Java Programming Language?
The String class was conceived as an immutable class (and this decision was intentionally thought-out). But String concatenation is really slow, I’ve benchmarked it myself. So the StringBuffer was born. Really great class, synchronized and really fast. But some people were not happy with the performance cost of some synchronized blocks, and the StringBuilder was introduced.
But, when using String to concatenate not too many objects, the immutability of the class makes it a really natural way to achieve thread-safety. I can understand the use of StringBuffer when we want to manage several Strings. But, here is my first question:
-
If you have, say, 10 or fewer strings that you want to append, for example, would you trade simplicity for just some milliseconds in execution time?
I’ve benchmarked StringBuilder too. It is more efficient than StringBuffer (just a 10% improvement). But, if in your single-threaded program you’re using StringBuilder, what happens if you sometimes want to change the design to use several threads? You have to change every instance of StringBuilder, and if you forget one, you’ll have some weird effect (given the race condition that may arise) that can be produced.
-
In this situation, would you trade performance for hours of debugging?
Ok, that’s all. Beyond the simple question (StringBuffer is more efficient than “+” and thread-safe, and StringBuilder is faster than StringBuffer but no thread-safe) I would like to know when to use them.
(Important: I know the differences between them; this is a question related to the architecture of the platform and some design decisions.)
Nowadays both StringBuffer and Builder are sort of useless (from performance point of view).
I explain why:
StringBuilder was supposed to be faster than StringBuffer but any sane JVM can optimize away the synchronization. So it was quite a huge miss (and small hit) when it was introduced.
StringBuffer used NOT to copy the char[] when creating the String (in non shared variant); however that was a major source of issues, incl leaking huge char[] for small Strings. In 1.5 they decided that a copy of the char[] must occur every time and that practically made StringBuffer useless (the sync was there to ensure no thread games can trick out the String). That conserves memory, though and ultimately helps the GC (beside the obviously reduced footprint), usually the char[] is the top3 of the objects consuming memory.
String.concat was and still is the fastest way to concatenate 2 strings (and 2 only… or possibly 3). Keep that in mind, it does not perform an extra copy of the char[].
Back to the useless part, now any 3rd party code can achieve the same performance as StringBuilder. Even in java1.1 I used to have a class name AsycnStringBuffer which did exactly the same what StringBuilder does now, but still it allocates larger char[] than StringBuilder. Both StrinBuffer/StringBuilder are optimized for small Strings by default you can see the c-tor
Thus if the 2nd string is longer than 16chars, it gets another copy of the underlying
char[]. Pretty uncool.
That can be a side effect of attempt at fitting both StringBuilder/Buffer and the char[] into the same cache line (on x86) on 32bit OS… but I don’t know for sure.
As for the remark of hours of debugging, etc. Use your judgment, I personally do not recall ever having any issues w/ strings operations, aside impl. rope alike structure for the sql generator of JDO impl.
Edit:
Below I illustrate what java designers didn’t do to make String operations faster.
Please, note that the class is intended for java.lang package and it can put there only by adding it to the bootstrap classpath. However, even if not put there (the difference is a single line of code!), it’d be still faster than StringBuilder, shocking? The class would have made string1+string2+… a lot better than using StringBuilder, but well…