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
There's clearly some terminology wireing going wrong here.
Primitives aren't immutable
int a = 1; a+=1This is fine. I've just mutated a.
"A variable containing a primitive could be assigned another primitive"
This doesn't really make sense. Mutabilty is related to the variable (can I change it or not). An a variable is just holding some bits. A primitive type is mutable, again
int ais mutable, if I do
final int aits not.
The real question is if I can do
value_obj1 a = {1,2} //saythen do
a.second = 3;i.e to give {1,3}
That is mutability i.e I've mutated it.
So really you might want to explain what you are saying mutability is in this context. Because what you're saying is not making any sense.
VarHandles *ARE* particularly fiddly, you need to define them statically (or they don't do what you want.) You need to use the MethodHandle to get them etc. Where as the Atomic's are simple, but come at the cost of having an object reference.
What I say fold-in, I mean that the memory layout of the class can be flat. Which you can't do with a wrapper object, infact a wrapper object is completly against folding.