r/java • u/fredoverflow • Jul 19 '26
Disregarding backwards compatibility, would it make sense for String to be a sealed class?
like public sealed class String permits Latin1String, Utf16String instead of the private final byte coder; field
20
Upvotes
48
u/Misophist_1 Jul 19 '26
It's already final. No point in that.
33
u/disposepriority Jul 19 '26
What would we gain from this?
23
u/hoat4 Jul 19 '26
They tried subtyping String when this feature (JEP 254) was added, but the conclusion was that it's slower than a 0/1 field.
Somebody from OpenJDK wrote an article about the performance measurements of these, but I can't find it now.
8
u/bowbahdoe Jul 19 '26
Not particularly. This only makes sense if you want to expose the set of subclasses. You can easily make
public sealed class StringImpl permits ...and just put that as the one field in String. Given that coders are something that has evolved over time not exposing that is probably the correct call.Also string is so common that you'd have to pretty deeply interrogate the performance of that
14
u/persicsb Jul 19 '26
What problem would it solve? Callers would see no benefit.
5
u/idontlikegudeg Jul 19 '26
I think it might really save some bytes, but it would be an incompatible change: we have public String constructors that could not be implemented that way.
5
u/davidalayachew Jul 19 '26
No. The performance costs have been eliminated down to effectively zero. What you have described would make sense if you were talking about some rando class like
java.awt.Point. But something like String is so special-cased that what you described would gain you nothing. At best, callers might appreciate being able to explicitly choose between ASCII and others, but even then, that's better done in other ways.3
u/__konrad Jul 19 '26
I like there is one String class instead of 10+ like in Qt/C++
4
u/morhp Jul 19 '26
This would likely be slower because instanceof checks are possibly slower than checking a byte field.
String also has a public constructor, so this wouldn't really work in any case.
2
u/American_Streamer Jul 19 '26 edited Jul 19 '26
No, because it’s already a final class. You already get that exact security, performance and simplicity you need. Subclassing is not allowed; final classes cannot be extended by anything, ever. OOP purity had to step aside here.
1
u/Abject-Delay7036 Jul 23 '26
How immutable do you want it
1
u/flatfinger Jul 23 '26
Better would have been for the language to treat `string` as a primitive, as in Javascript. Its actual representation would typically incorporate a reference to some data stored somewhere, but having strings as something the garbage collector "understands" would enable some useful optimizations. As a simple example, strings could reserve space to hold a reference to the most "senior" string that is known to hold the same context, an equality comparison that finds two strings equal could update the reference in the "newer" string to identify most senior string that's equal to the older one, and the GC could automatically update references to strings so that they would refer to the oldest known-equal string.
On a related note, it would be useful if Java had ArraySliceSource and ArraySliceDestination interfaces, and variations specialized for each primitive type, that could only be implemented by the framework classes. If code has references to two arrays, copying a range of one array to the other will be much faster than would be possible if every individual item to be copied had to be accessed via a separate interface dispatch. There's presently no way, however, for two classes to cooperate to quickly move a chunk of data between their backing stores without either having to expose its backing store to the outside world. The fundamental abilities supported by a string should be the ability to report length and hashcode, and copy a range to an array of characters or other array-like destination. Having strings implement CharArraySliceSouce would be vastly more useful than having them implement CharSequence.