Android: enum vs static final ints?

后端 未结 4 1437
闹比i
闹比i 2021-02-01 15:33

What are the advantages (or disadvantages) of having an enum versus having a set of static final ints in Java Android applications? Are there efficienc

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-01 16:26

    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.

提交回复
热议问题