I saw a discussion of the work and commit history behind Valhalla value objects. What do they change in Java, and why does bringing them into the JDK take so much work?
What are Valhalla value objects, and why are they such a big JDK change?
Answers
A useful starting point is double versus Double. The primitive holds a numeric value, while Double is an object and can be used where Java expects objects. Valhalla aims to let programmers define object types that retain some of that convenience while giving the runtime more opportunities to optimize their storage and use.
That does not mean every value object is guaranteed to be as fast as a primitive. The actual optimization depends on the runtime and how the value is used.
Reply to this answer
The key semantic difference is identity. Ordinary objects have an identity distinct from their field values. A value object gives up that identity, so the runtime has more freedom in how it represents it.
Two possible optimizations are flattening, where values can be stored directly in a containing object or array instead of through separate references, and scalarization, where the runtime works with individual fields rather than materializing an object. Neither is a promise that every instance will be flattened or scalarized.
Reply to this answer
This is more than adding a keyword to the compiler. The language rules and runtime representation have to work together, and optimizations have to preserve the behavior Java programs are entitled to expect. That makes the interactions with existing features and implementations a big part of the job.
A commit count by itself is not a measure of difficulty, though. It includes many individual changes across the history of the work, not a neat count of separate obstacles.
Reply to this answer
There is also a distinction between preview and incubation. The discussion described value objects as a preview feature, not an incubator feature. Preview rounds let people try a feature and provide feedback before it is finalized.
The number of rounds is not fixed in advance. It can change as problems are found in the implementation or in how the feature interacts with other parts of Java.
Reply to this answer
Add your answer
Answers are reviewed before publication.
r.691!gimbalcrank:
Does that mean equality is just whatever equals() says?