Java: Enum vs. Int

前端 未结 9 2306
野的像风
野的像风 2020-11-27 14:11

When using flags in Java, I have seen two main approaches. One uses int values and a line of if-else statements. The other is to use enums and case-switch statements.

<
9条回答
  •  眼角桃花
    2020-11-27 14:47

    Memory usage and speed aren't the considerations that matter. You would not be able to measure a difference either way.

    I think enums should be preferred when they apply, because the emphasize the fact that the chosen values go together and comprise a closed set. Readability is improved a great deal, too. Code using enums is more self-documenting than stray int values scattered throughout your code.

    Prefer enums.

提交回复
热议问题