forum

Home / DeveloperSection / Forums / Why switch is faster than if

Why switch is faster than if

Anonymous User 1441 04-Dec-2015
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?

Updated on 04-Dec-2015
I am a content writter !

Can you answer this question?


Answer

1 Answers

Liked By