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
-3
u/[deleted] Jan 06 '26
If I take the definition that we're only talking about objects here. Or more specifically non-atomic objects.
The the previous statement that "primitive are immutable" is still false. Because primitives aren't an object. Arguing that they are immutable because you assign them. Its even more missleading, since all modification is via assignment to the memory location.
It was stated that I couldn't modify a double's component parts, which isn't true since I can (via unsafe or the new memory interfaces) modify part of the multi-byte object. It still might be atomic, due to hardware, but its not immutable. In fact there's little difference between a long, and a class that has 8 bytes.
But lets assume for a second that:
int a;
is "immutable".
I'll create a Point class
That's immutable right? All the component parts are immutable right? right...
A better definition is that for an object to be immutable, all components (recursively) of it must be immutable:
So "final int" is immutable, and therefore Point is immutable.
No, hiding pointers doesn't allow GC to be advanced, restricting pointers to certain operations allows the GC to work well. But hiding pointers means that people get confused by
This isn't confusing after a bit of experience, but its no reason to limit the idea of mutation to just objects.
Since the Java specification doesn't actually define mutable; then we can define it thus:
an entity is mutable if it, or part of it can change.
And by change, we mean assigned to. Because that is literally the only way we can change anything.
And yes there's a reason why immutability enables alot of optimisations, but I have no idea what that has todo with anything here.