I'm reading the Project Valhalla proposal to add value types to Java. It's arguing for explicit value types because the compiler can't automatically move allocations from the heap to the stack very well even if the objects are immutable (all fields are final) because it can't know if some code won't suddenly check the object for reference equality or try to lock on it. Object identity must be preserved, and with it heap allocation, garbage collection, and a boatload of overhead for small objects that are allocated in tight loops.My question is why can't the object's identity be preserved on the stack as well? I mean, issue each object a unique id, and use it as its identity instead of its address. Immutable objects can then be always passed by value and never garbage collected.It seems like a much simpler and more palatable solution than value types, even if some objects grow by another 8-12 bytes. I've used C#, and explicit value types are a pain. They add a lot of conceptual bloat to a language.
Question about Java's proposed value types
java