Java: Performance of Enums vs. if-then-else

后端 未结 6 1742

I\'ve had no real luck getting a concise answer for this comparison by using Google and rather than do my own time consuming evaluations, I thought I would ask first.

<
6条回答
  •  时光取名叫无心
    2020-12-17 21:21

    In theory a switch statement can be optimised as a single calculated jump, whereas if-then-else chains have to remain as individual comparisons. I don't know whether Java actually performs this optimisation though.

    Regardless, switches are better than if-then-else chains in terms of readability and maintainability, so use them anyway if possible.

提交回复
热议问题