Android: enum vs static final ints?

2020-05-19 04:36发布

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.

4条回答
够拽才男人
2楼-- · 2020-05-19 05:04

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;
查看更多
该账号已被封号
3楼-- · 2020-05-19 05:05

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);
查看更多
欢心
4楼-- · 2020-05-19 05:11

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.

查看更多
走好不送
5楼-- · 2020-05-19 05:17

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.

查看更多
登录 后发表回答