Performance difference of if/else vs switch statement in Java

0 votes

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,380 points
232 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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,320 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 26, 2018 in Java by code.reaper12
• 3,450 points
812 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,190 points
97 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,040 points
495 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,840 points
147 views
0 votes
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,520 points
37 views
0 votes
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,280 points
27 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,190 points
135 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,190 points
34 views
0 votes
1 answer

Fetch list of in-between dates using Java

I tried generating the list of the ...READ MORE

answered May 10, 2018 in Java by geek.erkami
• 2,320 points
43 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,320 points
21 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.