B
Brian
I'd like to learn more about enums in 1.5. I'm a bit concerned about
the efficiency of an enum versus a final static variable. I have a
fairly tight inner loop that isn't running as fast as I'd like it
to and I suspect it's enums (lots of 'em) creating calls at runtime.
Here's a nonsensical example loosely based on dice. It's simple. My
guesses are included as to what is a compile time literal versus a
runtime call. Am I right?
Thanks.
public class Test {
public enum Axis {
RIGHT,
FRONT,
TOP,
BOTTOM,
BACK,
LEFT;
}
public enum Face {
ONE(Axis.RIGHT,"One"),
TWO(Axis.FRONT,"Two"),
THREE(Axis.TOP,"Three"),
FOUR(Axis.BOTTOM,"Four"),
FIVE(Axis.BACK,"Five"),
SIX(Axis.LEFT,"Six");
private Axis position;
private String label;
Face(Axis position, String label) {
this.label = label;
this.position = position;
}
public String getLabel() {
return label;
}
public Face opposite() {
switch (this) {
case ONE : return SIX;
case TWO : return FIVE;
case THREE : return FOUR;
case FOUR : return THREE;
case FIVE : return TWO;
case SIX : return ONE;
}
return null;
}
}
public static void main(String[] args) {
// Below are examples and my guess on when
// various "literals" are resolved
// Ex1. Definitely should be resolved at compile time
Axis myaxis = Axis.RIGHT;
// Ex2. Definitely should be resolved at compile time
Face mydie = Face.ONE;
// Ex3. Should be compile time
String mystring = mydie.getLabel();
// Ex4. Could be be compile time using a clever compiler
Face opposite = mydie.opposite();
}
}
the efficiency of an enum versus a final static variable. I have a
fairly tight inner loop that isn't running as fast as I'd like it
to and I suspect it's enums (lots of 'em) creating calls at runtime.
Here's a nonsensical example loosely based on dice. It's simple. My
guesses are included as to what is a compile time literal versus a
runtime call. Am I right?
Thanks.
public class Test {
public enum Axis {
RIGHT,
FRONT,
TOP,
BOTTOM,
BACK,
LEFT;
}
public enum Face {
ONE(Axis.RIGHT,"One"),
TWO(Axis.FRONT,"Two"),
THREE(Axis.TOP,"Three"),
FOUR(Axis.BOTTOM,"Four"),
FIVE(Axis.BACK,"Five"),
SIX(Axis.LEFT,"Six");
private Axis position;
private String label;
Face(Axis position, String label) {
this.label = label;
this.position = position;
}
public String getLabel() {
return label;
}
public Face opposite() {
switch (this) {
case ONE : return SIX;
case TWO : return FIVE;
case THREE : return FOUR;
case FOUR : return THREE;
case FIVE : return TWO;
case SIX : return ONE;
}
return null;
}
}
public static void main(String[] args) {
// Below are examples and my guess on when
// various "literals" are resolved
// Ex1. Definitely should be resolved at compile time
Axis myaxis = Axis.RIGHT;
// Ex2. Definitely should be resolved at compile time
Face mydie = Face.ONE;
// Ex3. Should be compile time
String mystring = mydie.getLabel();
// Ex4. Could be be compile time using a clever compiler
Face opposite = mydie.opposite();
}
}