Strings in Switch

Paul Benedict pbenedict at apache.org
Tue Dec 8 09:00:26 PST 2009


Jon,

On Tue, Dec 8, 2009 at 10:47 AM, Jonathan Gibbons
<Jonathan.Gibbons at sun.com> wrote:
> If hell were to freeze over, and String.hashCode were to change in JDK n, n
>>=8, then javac could emit different code for Strings in switch, depending
> on the value of -target.

Regarding the state of hell, I don't think a compiler implementation
should ever rely on such a gamble. The implication is obvious: if JDK
N makes a change (by Oracle, by some future owner of OpenJDK -- who
knows what happens 10+ years from now), then class files using the
OpenJDK de-sugaring would break. The emitted hash results would no
longer match the runtime hashes and execution would be unpredictable.

To safely emit hash results into byte code, I think you obviously need
to go the extra stretch and make a ruling on the algorithm never
changing. Isn't that just simply called being responsible?

Paul



More information about the coin-dev mailing list