---
title: "Why switch is faster than if"  
description: "Why switch is faster than if"  
author: "Anonymous User"  
published: 2015-12-04  
updated: 2015-12-04  
canonical: https://www.mindstick.com/forum/33680/why-switch-is-faster-than-if  
category: "java"  
tags: ["java"]  
reading_time: 1 minute  

---

# Why switch is faster than if

I have found lots of [books](https://www.mindstick.com/articles/12965/7-books-students-should-read-in-2018) in java saying [switch statement](https://www.mindstick.com/forum/55166/how-to-use-the-switch-statement-in-java) is [faster](https://yourviews.mindstick.com/story/1515/5-ways-to-get-in-shape-faster) than [if else statement](https://www.mindstick.com/forum/55162/how-to-use-if-else-statement-in-java). But I didnot find antwhere saying why switch is faster than if.\
ExampleI have a situation i have to [choose](https://www.mindstick.com/articles/13030/how-to-choose-the-best-online-bitcoin-casino) any one item out of two i can use either of the following way\

```
switch(item){case BREAD:     //eat Breadbreak;default:    //leave the restaurant}
```

or using [if statement](https://www.mindstick.com/forum/12682/jquery-toggle-if-statement) like the following\

```
if(item== BREAD){//eat Bread}else{//leave the restaurant}
```

considering item and BREAD is [constant](https://www.mindstick.com/forum/34714/difference-between-statics-vs-constant-in-c-sharp) [int value](https://www.mindstick.com/forum/159627/get-the-int-value-from-the-enum-in-c-sharp)\
In the above example which is faster in [action](https://www.mindstick.com/forum/155752/what-is-action-result-with-its-types) and why?

## Replies

### Reply by Anonymous User

Because there are special bytecodes that allow efficient [switch](https://www.mindstick.com/blog/104495/switch-bringing-new-advancements-in-the-education-management-system) statement evaluation when there are a lot of cases.\
If implemented with IF-statements you would have a check, a jump to the next clause, a check, a jump to the next clause and so on. With switch the JVM loads the value to compare and iterates through the value table to find a match, which is faster in most cases


---

Original Source: https://www.mindstick.com/forum/33680/why-switch-is-faster-than-if

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
