The differences between StringBuilder and StringBuffer in Java are well documented and have been affected in StackOverflow .
Basically, StringBuilder is an unsynchronized copy of StringBuffer , with almost the same interface, as it was intended as a faster replacement for StringBuffer . Their APIs are nearly identical, and they are in fact subclasses of the same inaccessible abstract class in the current JDK.
Therefore, I am very interested in why they are not connected with the public. Having both classes implements a common interface, or even having StringBuffer as a subclass of StringBuilder makes sense, allowing the existence of common code for both classes.
So why is this forced separation? Was it that programmers could not accidentally mix thread safety with unsafe code? Or was it just a design oversight that will now be inherited until the end of eternity?
EDIT:
To make everything clear: I can reason about why this is so, but I hope for specific links to the actual solution, for example. during the JSR process. Everything that could shed light on what constitutes a situation for me, which sometimes causes a certain difficulty.
EDIT 2:
The fact that both classes implement Appendable is completely crazy. Probably because this particular interface is useless for most purposes - it can only add one character or prepared object and that is it. In most cases, this is no more useful than both classes being subclasses of Object .
EDIT 3:
So, here is the rationale for this particular question from a semi-official source :
Evaluation by the library team:
For strings, StringBuffer and StringBuilder do not share a common public supertype. They are not intended for alternatives: one error (StringBuffer), and another (StringBuilder) is a replacement.
Obviously, the lack of a common supertype can in some cases slow down, hoping to port from StringBuffer to StringBuilder. The flip side is that by adding a common super-type, we accept the mistakes of our past and fixing them in the public interface, to be with us for all the time. This does not just slow down migration: it negates it.
java stringbuilder stringbuffer design-patterns
thkala
source share