Enumerations vs int flags?

2019-07-27 03:20发布

问题:

Just wanted some opinions on the matter. I have always used int flags, and was just curious on possible performance of ease of use if I were to use enumerations in Java?

回答1:

I very much doubt you'd see performance benefits - and in some cases there may even be performance penalties, depending on how you're using them. I doubt they'd be significant though.

The benefit isn't in terms of performance - it's in terms or expressing your intentions more clearly, leading to more readable (and type-safe) code. For example, with integer flags nothing's going to complain if you try to use (say) HTTP_STATUS_UNAUTHORIZED as a value for your file sharing mode in a method call... whereas if both of those were enums, the parameter would be strongly typed to really only allow file sharing modes (or null, admittedly, assuming you are talking about Java).



回答2:

As usual, Effective Java says it best:

Programs that use the int enum pattern are brittle. Because int enums are compile-time constants, they are compiled into the clients that use them. If the int associated with an enum constant is changed, its clients must be recompiled. If they aren’t, they will still run, but their behavior will be undefined.

There is no easy way to translate int enum constants into printable strings. If you print such a constant or display it from a debugger, all you see is a number, which isn’t very helpful. There is no reliable way to iterate over all the int enum constants in a group, or even to obtain the size of an int enum group.

(+ 10 more pages of why enums are better :-))

Source: Item 30: Use enums instead of int constants



回答3:

Enumerations more clearly express what you are trying to do. Use them and do not worry about performance; performance impact will be negligible.



回答4:

An equivalent of using bit mask flags is to use EnumSet (which happens to use a bit mask)

enum Colour { RED, GREEN, BLUE }
Set<Colour> colours = EnumSet.noneOf(Colour.class);
colours.add(Colour.RED);
colours.add(Colour.GREEN);
if (colours.contains(Colour.RED)) // true
   System.out.println(colours+" contains RED");

if (colours.contains(Colour.BLUE)) // false


回答5:

They're basically the same thing except enumerations are self-documented. I recommend using enumerations as soon as someone else might be in contact with your code.



回答6:

An Enum a type with its own methods and optimized containers. You should prefer it over int flags because it's safer and possibly more efficient (though I doubt there's very much in it at all!).



回答7:

In addition to the type-safe code, you have a single-reference to share the collection (re-usability)