Performance difference of if else vs switch statement in Java

+1 vote

In terms of performance of the application, which among if/else and switch statement will give better results?

Jul 26, 2018 in Java by misc.edu04
• 1,450 points
3,770 views

1 answer to this question.

0 votes

The thing you are worried about is micro-optimization and premature optimization.

Rather than worrying about this, I would suggest taking the readability and maintainability of the code more into consideration.

If there are more than two if/else blocks glued together or its size is unpredictable, then go for a switch statement.

As an alternate solution, you can also use Polymorphism.

For that, first create some interface:

public interface Test { void execute(String input); }

And get hold of all implementations in some Map. You can do this either statically or dynamically:

Map<String, Test> test= new HashMap<String, Test>();

Finally, replace the if/else or switch by something like this (leaving trivial checks like null pointers aside):

test.get(name).execute(input);

It might be micro slower than if/else or switch, but the code is at least far better maintainable.

answered Jul 26, 2018 by geek.erkami
• 2,680 points

Related Questions In Java

0 votes
1 answer

“Missing return statement” within if / for / while in Java

Putting a return statement in any of the ...READ MORE

answered Sep 27, 2018 in Java by code.reaper12
• 3,500 points
9,100 views
0 votes
2 answers

One line initialization of an ArrayList object in Java

In Java 8 or earlier: List<String> string = ...READ MORE

answered Jul 26, 2018 in Java by samarth295
• 2,220 points
4,813 views
0 votes
3 answers

Check if a String is numeric in Java

Java 8 Lambda Expression is used: String someString ...READ MORE

answered Sep 3, 2018 in Java by Daisy
• 8,140 points
3,740 views
0 votes
2 answers

Counting no of Occurrence of a particular character inside a string in Java

We can find out the no. of ...READ MORE

answered Sep 7, 2018 in Java by Sushmita
• 6,920 points
2,818 views
+1 vote
1 answer

Are arrays equivalent to objects in Java ?

Yes; the Java Language Specification writes: In the Java ...READ MORE

answered May 10, 2018 in Java by Rishabh
• 3,620 points
1,347 views
+1 vote
1 answer

Remove objects from an array in Java?

We can use external libraries: org.apache.commons.lang.ArrayUtils.remove(java.lang.Object[] array, int ...READ MORE

answered Jun 26, 2018 in Java by scarlett
• 1,290 points
1,210 views
+1 vote
3 answers

What is the syntax to declare and initialize an array in java?

You can use this method: String[] strs = ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,220 points
3,623 views
0 votes
2 answers

What is the syntax to initialize an array?

Rather than learning un-Official websites learn from ...READ MORE

answered Aug 2, 2018 in Java by samarth295
• 2,220 points
1,048 views
0 votes
2 answers

Fetch list of in-between dates using Java

java.time Package The new java.time.package in Java 8 incorporates ...READ MORE

answered Aug 21, 2019 in Java by Sirajul
• 59,230 points
5,854 views
0 votes
1 answer

Working of post increment operator in Java

Well, I think the confusion is because ...READ MORE

answered May 23, 2018 in Java by geek.erkami
• 2,680 points
832 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP