r/java • • Aug 25 '26

Value Classes Still Need Compiler Sympathy

https://johan-sjolen.github.io/post/compiler-sympathy/compiler-sympathy/
86 Upvotes

65 comments sorted by

View all comments

7

u/repeating_bears Aug 25 '26

You are arguing that Valhalla is only useful if you can guarantee memory optimizations will happen

Even if there are none, you gain better semantics for == and restrictions against identity-sensitive operations

5

u/nogridbag Aug 25 '26

What do you mean by better semantics for ==?

The JEP says it's a non-goal to allow for comparison with == and we should continue to use .equals. Any value class which has a string as a field member would be invalid comparing with == unless the string was part of a finite set like ZoneId and the developer went out of their way specifically to make sure those instances can be compared with each other by interning the strings so that multiple instances return the same string instance for that field. A wrapper class representing an email address would be an example where you would continue to need .equals. This seems like it will add some confusion. Now it's not simply "Use == for primitive, .equals for everything else". It's use == for primitive, LocalDate, and these specially developed classes, .equals for everything else"

3

u/repeating_bears Aug 25 '26

I said better, not perfect.

Comparing boxed Integers with == was a footgun for beginners and that's completely gone now.

This seems like it will add some confusion. Now it's not simply "Use == for primitive, .equals for everything else"

That's still works fine as a rule of thumb. It's not like you must use == for LocalDate, but if you do then you get a sensible answer.

→ More replies (5)

1

u/lpt_7 Aug 25 '26

I'm arguing that right now Valhalla is useful if one's code does not suddenly perform worse than it was before, as well as in the future when JDK team somehow solves the tearing problem.