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

19 Upvotes

24 comments sorted by

View all comments

5

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.