r/java • u/nlisker • Jul 14 '26
New candidate JEP: 401: Value Objects (Preview)
https://mail.openjdk.org/archives/list/valhalla-dev@openjdk.org/thread/IUNJJ3WP3X3XHP3QZTXXSSCPKFDNTK3W/
87
Upvotes
View all comments
Show parent comment
3
u/FirstAd9893 Jul 15 '26
If the String is treated as a simple wrapper around a
char[], then it could conceivably be a value class. However, the==operator would only return true for Strings which refer to the exact samechar[]reference. To fix this, the String constructor would need to rely on an intern pool ofchar[]instances, but this would add a huge performance penalty.It should noted that arrays cannot be value classes, because they're mutable, and because they have a variable size. The optimizations which make value classes attractive wouldn't work.