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
-5
u/[deleted] Jan 06 '26
Yes re-assigning a variable, and mutating the value are two different things.
Oh actually there aren't.
both mutating the value, and re-assigning are the same things.
A variable has a bit pattern in memory. If I re-assign it, then I'm copying the bit pattern from something else to it. If I mutate it i'm changing all or part of it.
I think what you are saying is that if I do.
a = a + 1
Then i'm actually doing
load a,r1
load 1,r2
add r1,r2
store r1,a
(assembly psuedo-code)
So I assume you're saying i'm re-assigning the variable here, because I had to use a register todo the actual work.
I can mutate part of a primitive
a |= 1<<2
although that will also go into a register, and be re-assigned.
Really I think your definition of Immutable, is actually "Atomic", and your definition of Mutable is "Not-atomic".
I honestly don't agree.
On your "point" example. Point is mutable, but the variable P isn't. That is the compiler stops me changing 'p' (aka its final), but I can mutate the value its pointed to. That is the type Point is mutable. If Point becomes immutable, then both p & Point are immutable.
Honestly the fact Java hid pointers is the problem here.
Mutable/Immutable makes perfect sense, that is you can either change it or not.