I have found lots of books in java saying switch statement is faster than if else statement. But I didnot find antwhere saying why switch is faster than if.
Example
I have a situation i have to choose any one item out of two i can use either of the following way
switch(item){
case BREAD:
//eat Bread
break;
default:
//leave the restaurant
}
or using if statement like the following
if(item== BREAD){
//eat Bread
}else{
//leave the restaurant
}
considering item and BREAD is constant int value
In the above example which is faster in action and why?
Anonymous User
04-Dec-2015