Remix.run Logo
▲ wpollock 2 days ago

> But it uses flyweight instances for values up to 256, so in Java, whether two Integers with the same numeric value are equal according to the = operator depends on what that value is. ...

I believe Java caches Integer objects for values -128..+128, not 256. But that may be an implementation detail, and not something to count on.

▲bunderbunder 2 days ago | parent | next [-]

No, you’re right. I misremembered.

▲wpollock 2 days ago | parent [-]

> No, you’re right. I misremembered.

We both misremembered. I found where this is documented and the range is -128..127 inclusive: <https://docs.oracle.com/en/java/javase/27/docs/api/java.base...>

▲gf000 2 days ago | parent | prev [-]

And since java 28's value types, == will work identically for all Integers, finally.

▲wpollock 2 days ago | parent [-]

Note that value types will still be a "preview" feature in Java 28.

I'm looking forward to structured concurrency in Java 28. I already thought of a title for my article "Java Concurrency - Past, Present, and Futures".