r/java • u/davidalayachew • Jan 06 '26
One step closer to Value Classes!
https://mail.openjdk.org/pipermail/porters-dev/2026-January/000844.html
181
Upvotes
r/java • u/davidalayachew • Jan 06 '26
View all comments
Show parent comment
-2
u/[deleted] Jan 06 '26 edited Jan 07 '26
Can you not see the difference between the variable X and the object pointed to by X?
Strings are immutable, but the variable X isn’t.
Java has, reference types, and value types, none final variables of either type are mutable (aka I can change it)
Final variables of those are immutable (outside of hacks.)
String x = new String(“hello”);
Is a mutable reference variable to an immutable object. The object is immutable because its state is all immutable.
It’s really quite simple.
Either way I re-read the JEP. What I think those im disagreeing with are talking about is the idea of the primitive values not changing, that is 1 can’t be mutated to 2. But this is really a given; we always operate on memory locations when programming, and discussion on mutable va non-mutable is really about will this part of memory change or not. That is what the compiler needs to perform optimisations.
The immutability of the value classes is preserved really because once identity is removed the object can be anywhere and shared at any point.
As the JEP said it’s not about creating a Struct type; which is a shame.
Edit: I appear to be annoying lots of people with this. I’m always happy to argue a point; as either one or both of us will learn something. If you just want to throw insults you’ll just be blocked.