Why not create a StringBuilder and StringBuffer common interface?

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.

+8
java stringbuilder stringbuffer design-patterns
source share
2 answers

I have no reference to JSR, but from my experience. Below are a few reasons:

  • StringBuffer as a subclass of StringBuilder not a good idea for performance reasons . As for creating StringBuffer streaming security, you have to mask every call to StringBuilder , which is a lot of overhead.

  • By adding to the previous paragraph, you can further optimize if you have direct access to the internal elements of the class , so Java added java.lang.concurrent on top of java.util.Collections.synchronized* apis. Since more direct access provides more opportunities for optimization. To confirm this item Link from IBM Blog

  • Further adding to the first point, I do not think that this is design supervision, since both classes are final , so they definitely do not want these classes to be subclassed. p>

  • For the same interfaces, both classes implement the same interfaces ie Serializable, Appendable, CharSequence . Thus, they are replaced. The only thing is that they do not implement one common interface, but instead three common interfaces. Which makes sense, since there is no need to have one bloated interface, which technically will be the sum of the current interfaces ( Serializable, Appendable, CharSequence ).

EDIT:

  • Point @MatthewFlaschen that there are apis, which are the same b / w StringBuffer and StringBuilder , but not in any of the implemented interfaces. This is more related to backward compatibility. You want to add a new api, but the interface is used by many other classes, so changing the interface may not be possible soln. This is a well thought out solution that Java guys could make. Therefore, I will not say that this is a mistake.

EDIT 2:

Note: It should be noted that StringBuffer was introduced in 1.0 and StringBuilder in 1.5. Thus, apis, which is present in both classes, but not in the interfaces, is introduced later, and not during the creation of these classes too.

+3
source share

In fact, they implement Appendable .

I do not agree that it is useless. In my experience, most of the use of StringBuilder / StringBuffer deals with strings (which implement CharSequence ). For the remainder, you can call String.valueOf before passing it.

It would be convenient if there was another interface that also had other methods, such as append(long) . But it is not important.

It would be wise to have a common interface. They have different performance and thread characteristics, but this is fine and true for many interfaces in the JDK.

For example, CopyOnWriteArrayList in a CopyOnWriteArrayList safe list based on an array (it creates a new list for each record), and LinkedList is a non-stream linked list.

+2
source share

All Articles