31

What are the advantages (or disadvantages) of having an enum versus having a set of static final ints in Java Android applications? Are there efficiency or performance optimizations that occur that favor the use of one or the other?

I ask this in context of say intent requestCodes and such - which tend to be ints in the Android sample code, as opposed to values from an enum, which I was used to in C.

Cubic
  • 358
  • 1
  • 3
  • 9
  • enums can be null, ints can't – Erik Mar 09 '11 at 21:38
  • 14
    See [Why doesn't Android use more enums?](http://stackoverflow.com/questions/4822877/why-doesnt-android-use-more-enums) and [Why was “Avoid Enums Where You Only Need Ints” removed from Android's performance tips?](http://stackoverflow.com/questions/5143256/why-was-avoid-enums-where-you-only-need-ints-removed-from-androids-performance) and [How much memory do Enums take?](http://stackoverflow.com/questions/143285/how-much-memory-do-enums-take) – Josh Lee Mar 09 '11 at 21:38
  • 2
    Write the code in cleanest way you can first; then if you discover you have a performance problem, and you discover it's not IO, consider breaking your code for performance sake – iluxa Mar 09 '11 at 21:47

4 Answers4

29

Enum advantages from this question:

  • They are much more type-safe than integers, strings, or sets of boolean flags.
  • They lead to more readable code.
  • It's more difficult to set an enum to an invalid value than an int or string.
  • They make it easy to discover the allowed values for a variable or parameter.
  • Everything I've read indicates that they perform just as well as integers in C# and most JVMs.

I would add:

  • Enums can have member and instance variables, whereas an int can't.

Like most abstractions, they are generally unequivocally advantageous once their performance catches up. Especially in your application code (as opposed to framework code) I would choose enums over other methods that simulate them.

Community
  • 1
  • 1
Matthew
  • 44,826
  • 10
  • 98
  • 87
  • Thanks for the link re: android docs, I read that a while ago and always wondered about it. – Ozzy Jun 28 '13 at 22:36
12

A very simple answer from personal experiences would be that Enums provide much better type safety or in other words the compiler gets to play a more active role in keeping your code bug free.

On the other hand, because Enums are "second-class citizens" of the object world, they can be difficult to use in some of the more subtle design patterns commonly used today, especially when generics are involved.

And finally, you can use static final ints in a bitfield. you couldnt do the following with an enum:

int selectedOptions = Options.OPTION1 | Options.OPTION2 | Options.OPTION3;
Nick
  • 8,181
  • 4
  • 38
  • 63
  • While I accepted the first response as the proper answer to the question, I feel that the last point you bring up is actually very good. I've never tried it myself, but it's a very good fact to know. Thanks! – Cubic Mar 10 '11 at 00:49
  • 6
    On that last part, using an EnumSet is the recommended way to go. I've never much liked the bit flag pattern. – Spektr Dec 04 '11 at 04:41
  • Type safety is a big deal. You can get all the type-safety benefits of enums, while retaining backwards compatibility with existing code, by running a static analysis such as the [Fake Enum Checker](https://types.cs.washington.edu/checker-framework/current/checker-framework-manual.html#fenum-checker). If you don't care about backwards compatibility with existing code, then using an enum is usually better. – mernst Sep 03 '16 at 21:37
7

Well... according to a bald guy Enums are really bad for memory.

You should use @IntDef/@StringDef annotations:

public static final int NAVIGATION_MODE_STANDARD = 0;
public static final int NAVIGATION_MODE_LIST = 1;
public static final int NAVIGATION_MODE_TABS = 2; 

@IntDef({NAVIGATION_MODE_STANDARD, NAVIGATION_MODE_LIST, NAVIGATION_MODE_TABS})
public @interface NavigationMode {}

and then

@NavigationMode
public abstract int getNavigationMode();

public abstract void setNavigationMode(@NavigationMode int mode);
Cheborra
  • 2,627
  • 4
  • 23
  • 39
3

One advantage of ints over enums is in a CLASS FACTORY. The following C# code is not extensible:

class Factory
{
    public enum DrawableType {CIRCLE,SQUARE};
    public static Drawable GetInstance(DrawableEnum e)
    {
        if (e == DrawableType.CIRCLE)
        {
            return new Circle();
        }
        else if (e == DrawableType.SQUARE)
        {
            return new Square();
        }
        else
        {
            throw new IndexOutOfRangeException(); // should never get here
        }
    }

I wrote this poor code. Reviewing Design Patterns, the gang of four used an int. I tried to recover here.

JAL
  • 3,319
  • 2
  • 20
  • 17