Alex Rivera | Logout

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

Asked 2010-01-18T14:09:30.207
151

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

Edit
Report

1 Answer

9

I remember reading that there are 2 kinds of Switch statements in Java bytecode. (I think it was in 'Java Performance Tuning' One is a very fast implementation which uses the switch statement's integer values to know the offset of the code to be executed. This would require all integers to be consecutive and in a well-defined range. I'm guessing that using all the values of an Enum would fall in that category too.

I agree with many other posters though... it may be premature to worry about this, unless this is very very hot code.

answered 2010-01-18T15:42:15.807

Your Answer