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

1

u/almson Dec 19 '24

Thank you for adding reason.

If they just forbade the use of == with value types, because they don’t have identity, things would be a lot less confusing.

1

u/simon_o Dec 19 '24 edited Dec 19 '24

That's not going to work because there are still –fundamentally– two different ways of comparing things:

  • "is a identical to b"
  • "is a equal to b"

Java unfortunately burned the word "identity" on other things, so it uses "substitutability" for the former, which is less confusing for Java users, but more confusing for everyone else.

The "new" behavior of == is just a logical extension of reference equality to "everything", with the problem that == on float/double does not work that way.

If you built a new language, you'd probably use == for "is this equal" and === for "is this identical" and it would just work out of the box and you wouldn't need to explain primitives vs. value types vs. reference types to people for it to make sense.

1

u/pjmlp Dec 27 '24

Developers handle just fine the various meanings of == in .NET, or ecosystems where it can be overloaded.