Java: Enum vs. Int
Both ints
and enums
can use both switch or if-then-else, and memory usage is also minimal for both, and speed is similar - there's no significant difference between them on the points you raised.
However, the most important difference is the type checking. Enums
are checked, ints
are not.
Consider this code:
public class SomeClass {
public static int RED = 1;
public static int BLUE = 2;
public static int YELLOW = 3;
public static int GREEN = 3; // sic
private int color;
public void setColor(int color) {
this.color = color;
}
}
While many clients will use this properly,
new SomeClass().setColor(SomeClass.RED);
There is nothing stopping them from writing this:
new SomeClass().setColor(999);
There are three main problems with using the public static final
pattern:
- The problem occurs at runtime, not compile time, so it's going to be more expensive to fix, and harder to find the cause
- You have to write code to handle bad input - typically a
if-then-else
with a finalelse throw new IllegalArgumentException("Unknown color " + color);
- again expensive - There is nothing preventing a collision of constants - the above class code will compile even though
YELLOW
andGREEN
both have the same value3
If you use enums
, you address all these problems:
- Your code won't compile unless you pass valid values in
- No need for any special "bad input" code - the compiler handles that for you
- Enum values are unique
Memory usage and speed aren't the considerations that matter. You would not be able to measure a difference either way.
I think enums should be preferred when they apply, because the emphasize the fact that the chosen values go together and comprise a closed set. Readability is improved a great deal, too. Code using enums is more self-documenting than stray int values scattered throughout your code.
Prefer enums.
You may even use Enums to replace those bitwise combined flags like int flags = FLAG_1 | FLAG_2;
Instead you can use a typesafe EnumSet:
Set<FlagEnum> flags = EnumSet.of(FlagEnum.FLAG_1, FlagEnum.FLAG_2);
// then simply test with contains()
if(flags.contains(FlagEnum.FLAG_1)) ...
The documentation states that those classes are internally optimized as bit vectors and that the implementation should be perform well enough to replace the int-based flags.
One of the reasons you will see some code using int
flags instead of an enum
is that Java did not have enums until Java 1.5
So if you are looking at code that was originally written for an older version of Java, then the int
pattern was the only option available.
There are a very small number of places where using int
flags is still preferable in modern Java code, but in most cases you should prefer to use an enum
, due to the type safety and expressiveness that they offer.
In terms of efficiency, it will depend on exactly how they are used. The JVM handles both types very efficiently, but the int method would likely be slightly more efficient for some use cases (because they are handled as primitive rather than objects), but in other cases, the enum would be more efficient (because it doesn't need to go throw boxing/unboxing).
You would be hard pressed to find a situation in which the efficiency difference would be in any way noticeable in a real world application, so you should make the decision based on the quality of the code (readability and safety), which should lead you to use an enum 99% of the time.