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

Anth0 picture Anth0 · Jan 18, 2010 · Viewed 110.2k times · Source

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

Answer

Waverick picture Waverick · Jan 18, 2010

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.