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
465 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,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
1,384 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
168 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,050 points
729 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,880 points
286 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,540 points
65 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
53 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
434 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
55 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 in Java by Sirajul
• 29,740 points
98 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
32 views