compiling Enum.ordinal() into constant inside a method

Vitaly Davidovich vitalyd at
Fri Feb 27 18:08:03 UTC 2015

Hi guys,

Suppose we have an enum like this:

enum E { ONE, TWO }

Then we have a method like so:

static E valueOf(int ordinal) {
   if (ordinal == E.ONE.ordinal()) return E.ONE;
   if (ordinal == E.TWO.ordinal()) return E.TWO;
   throw new <some_exception>; // this path never taken

On 7u60 (sorry, can't easily test on a later version now), the compiled
code (C2) is actually loading the ordinal field for both comparisons.  Of
course doing the following:

static final int ONE = E.ONE.ordinal();
static final int TWO = E.TWO.ordinal();

static E valueOf(int ordinal) {
   if (ordinal == ONE) return E.ONE;
   if (ordinal == TWO) return E.TWO;
   throw new <some_exception>; // this path never taken

uses constant comparisons (well, the zero case is just a test, but 1 uses

Is there a good reason why the first version of the method cannot be
compiled into the second one? Apologies if this is actually the case on
later hotspot versions, in which case I'd be happy to hear that.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the hotspot-compiler-dev mailing list