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.
I'm fairly sure that a switch statement using Enums would perform faster than an if-then-else statement, though whether or not it is a noticable difference is another question.
Could someone shed some light on this for me?
Thanks for the quick responses guys, I will keep this in mind for future projects.
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.
I don't know about faster, I'd guess they are both blazingly fast.
My consideration is that switch with enums is a lot more readable than a multi-if/else block
But beware of missing break statements!!
Just stick with the most readable and clear to understand code you can come up with, I'm sure you lost all the time gained in the performance optimization while looking for this answer already. Micro-optimizations like this rarely are worth it and can easily result in code that's more complex than needed.
My answer to this is the same as always to question is language construct X generally faster than language construct Y: There is no general answer!
There may be only specific answer for certain implementations of a language, e.g. Oralce's (formally Sun's) Hotspot-compiler based JVM or for an IBM JDK on plattform Z or for an OpenJDK on Linux, or...
So, the only way to give a meaningfull answer to your question is to do a proper benchmark. Beware of micro benchmarks, they are more often wrong than right, see e.g. How not to write a micro benchmark. If you still want to find about use this question framework described here.
Therfore, I would advise to select language feature by their applicability and readibility in your context.
Yes, a switch statement will pretty much always execute faster than the equivalent block of if / else statements because the compiler can perform more optimisations (typically a switch block becomes compiled down to a branch table which is pretty much impossible to do with a block of conditionals.)
I'd say they're also both more readable and more maintainable (with the exception of using fall-through cases which I'd advise against!)
As to whether it's noticeably faster, it depends what you define as noticeable. Chances are unless you're after something really specific you won't notice it at all, but I'd still do things this way because of the readability advantage more than anything else (treat the speed advantage as a bonus!)
Yeap, it does, because in general term a switch statement works faster than if/else chain.
Although bytecode generated is not always definitive source for performance comparisons you can examine it to have a better idea.
For instance this code:
Generates the following:
Which seems to be pretty fast in both cases.
So, pick the one that is easier to maintain.