r/java • • 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

21 Upvotes

26 comments sorted by

View all comments

33

u/disposepriority Jul 19 '26

What would we gain from this?

18

u/hoat4 Jul 19 '26

Not OP, but memory usage reduction could be an advantage of subtyping: if no "coder" field needed, it's 8 byte saving for each String instance.

Another reason could be easier maintenance of java.lang.String for JDK developers.

7

u/FirstAd9893 Jul 19 '26

There's also the hashIsZero field, which will be combined with the coder byte into a single 4 byte field, not an 8 byte field. In order to reduce memory overhead further, both the coder and hashIsZero fields would need to be dropped.

When the coder field was added, the goal was to reduce memory overhead by using Latin-1 instead of UTF-16 in most cases. It's still a win overall.

1

u/Liskar-dev Jul 29 '26

it's 8 byte saving for each String instance

It's 4 bytes, because Compressed OOPs is on by default

-2

u/koflerdavid Jul 19 '26

It sounds like the debate about Pascal-style (length field) and C-style (\0 terminated) strings again. I guess it's simply not worth it in many cases. 8 bytes are really not that much compared to the length of most strings.

1

u/mikeblas Oct 26 '26

Oh! I've always been curious about this, and it sounds like you'll be able to help me: How long are most strings?

0

u/koflerdavid Oct 27 '26

8 bytes are eight characters in ISO-8859-1 encoding or four UTF codepoints in UTF-16. I hazard that most strings are rather longer, unless we're talking about specific applications or benchmarks where it could pay off to think about a more efficient encoding.

12

u/jesseschalken Jul 19 '26

Textbook X-Y problem

2

u/BillyKorando Jul 20 '26

I don't think the juice would be worth the proverbial squeeze, but I suppose some nice functional design concepts.

For example you could write:

switch(string){
   case Latin1String ls -> //Do latin string stuff
   case Utf16String us -> //Do UTF string stuff
}

Because the compiler knows about sealed classes, it can determine if a switch is exhaustive, so you wouldn't need to include a default. Which might also be nice if in the future a new sub-type of String is added.

Right now, you could check the value of coder, but you'd always need to include a default case with it, either literally with a switch or figuratively with a generic else, to handle if a new "type" is added.

Of course, that said, the benefit of this is pretty thin. That the accessor for coder is not part of the public API (package-private) is perhaps instructive that the encoding of a String is an implementation detail that general Java developers shouldn't concern themselves with.

And then, on top of that, the benefit is mostly in some minor functional program design benefits, I suppose maybe some JIT optimizations as well? Perhaps worth it if there was no, or at least minimal, backwards compatibility concerns, but that's obviously not the case here.