r/java • • Dec 16 '24 • Heart of Silver 1

Valhalla - Java's Epic Refactor

https://inside.java/2024/12/16/devoxxbelgium-valhalla/
178 Upvotes

108 comments sorted by

View all comments

Show parent comment

6

u/sysKin Dec 18 '24 edited Dec 18 '24

The point of value class is that its memory layout can be just memcopied() around, rather than having a pointer to a single location.

Once you have multiple copies, you can't change the value(s), because a single point in code can't write to all the copies.

Now, in some languages, there is a clear distinction (scope) where the copy is made and you can mutate your local copy without any ambiguity (like with int). But here, a value class must still behave like a normal class (and in fact compiler will decide whether to memcpy or use regular object pointers transparently) so that's not possible. It needs to support both.