Hello,
just a quick question, how much memory does an enum value need?
How about this example:
enum Figure{
EMPTY, WHITE_PAWN, BLACK_PAWN, WHITE_QUEEN, BLACK_QUEEN
}
Hello,
just a quick question, how much memory does an enum value need?
How about this example:
enum Figure{
EMPTY, WHITE_PAWN, BLACK_PAWN, WHITE_QUEEN, BLACK_QUEEN
}
So, enum is basically the size of int?
In your enum yes, each value is equivalent to an integer, Java enumerations are much more powerful than other languages so can be more complex. For instance this is an enumeration for a Bridge program I created
public enum Suit {
UNDEFINED ("?", "Undefined", -1),
C ("C", "Clubs", 0),
D ("D", "Diamonds", 1),
H ("H", "Hearts", 2),
S ("S", "Spades", 3),
NT ("NT", "No Trumps", 4);
private static final Suit[] types = {C, D, H, S, NT};
public final String shortDesc;
public final String desc;
public final int idx;
/**
*
* @param shortDesc
* @param desc
* @param idx
*/
private Suit(String shortDesc, String desc, int idx) {
this.shortDesc = shortDesc;
this.desc = desc;
this.idx = idx;
}
public static Suit type(char ch) {
switch (ch) {
case 'c':
case 'C':
return C;
case 'd':
case 'D':
return D;
case 'h':
case 'H':
return H;
case 's':
case 'S':
return S;
}
return UNDEFINED;
}
public static Suit type(int idx) {
return (idx >= 0 && idx <= 4) ? types[idx] : UNDEFINED;
}
}