What is the relative performance difference of if/else versus switch statement in Java?

前端 未结 8 1219
北恋
北恋 2020-11-22 16:25

Worrying about my web application\'s performances, I am wondering which of \"if/else\" or switch statement is better regarding performance?

8条回答
  •  遇见更好的自我
    2020-11-22 16:47

    I totally agree with the opinion that premature optimization is something to avoid.

    But it's true that the Java VM has special bytecodes which could be used for switch()'s.

    See WM Spec (lookupswitch and tableswitch)

    So there could be some performance gains, if the code is part of the performance CPU graph.

提交回复
热议问题