Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is String in switch statement more efficient than corresponding if-else statement?

Java documentation says

The Java compiler generates generally more efficient bytecode from switch statements that use String objects than from chained if-then-else statements.

AFAIK even String in switch uses .equals() internally in a case sensitive manner. So what efficiency do they mean in this context. Faster compilation? Less bytecodes ? better performance?

like image 262
Aniket Thakur Avatar asked Mar 01 '14 05:03

Aniket Thakur


People also ask

Why switch case is more efficient than if-else?

A switch statement is significantly faster than an if-else ladder if there are many nested if-else's involved. This is due to the creation of a jump table for switch during compilation. As a result, instead of checking which case is satisfied throughout execution, it just decides which case must be completed.

Are switch statements more efficient than if statements?

A switch statement is usually more efficient than a set of nested ifs. Deciding whether to use if-then-else statements or a switch statement is based on readability and the expression that the statement is testing.

How switch is faster than if-else?

Switch is generally faster than a long list of ifs because the compiler can generate a jump table. The longer the list, the better a switch statement is over a series of if statements.

What is the difference between switch statement and if-else if statement?

If-esle statement checks for equality as well as for logical expression . On the other hand, switch checks only for equality. The if statement evaluates integer, character, pointer or floating-point type or boolean type. On the other hand, switch statement evaluates only character or a integer datatype.


1 Answers

Using a switch statement is faster than equals (but only noticeably when there are more than just a few strings) because it first uses the hashCode of the string that switch on to determine the subset of the strings that could possibly match. If more than one string in the case labels has the same hashCode, the JVM will perform sequential calls to equals and even if there is only one string in the case labels that a hashCode, the JVM needs to call equals to confirm that the string in the case label is really equal to the one in the switch expression.

The runtime performance of a switch on String objects is comparable to a lookup in a HashMap.

This piece of code:

public static void main(String[] args) {     String s = "Bar";     switch (s) {     case "Foo":         System.out.println("Foo match");         break;     case "Bar":         System.out.println("Bar match");         break;     } } 

Is internally compiled to and executed like this piece of code:

(not literally, but if you decompile both pieces of code you see that the exact same sequence of actions occurs)

final static int FOO_HASHCODE = 70822; // "Foo".hashCode(); final static int BAR_HASHCODE = 66547; // "Bar".hashCode();  public static void main(String[] args) {     String s = "Bar";     switch (s.hashCode()) {     case FOO_HASHCODE:         if (s.equals("Foo"))             System.out.println("Foo match");         break;     case BAR_HASHCODE:         if (s.equals("Bar"))             System.out.println("Bar match");         break;     } } 
like image 152
Erwin Bolwidt Avatar answered Oct 19 '22 12:10

Erwin Bolwidt