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?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
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).
In addition to the type-safe code, you have a single-reference to share the collection (re-usability)
An equivalent of using bit mask flags is to use EnumSet (which happens to use a bit mask)
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.
As usual, Effective Java says it best:
(+ 10 more pages of why enums are better :-))
Source: Item 30: Use enums instead of
int
constantsEnumerations more clearly express what you are trying to do. Use them and do not worry about performance; performance impact will be negligible.