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
View all comments
Show parent comment
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.