Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Java's switch work under the hood?

How does Java's switch statement work under the hood? How does it compare the value of the variable being used, to those given in the case parts? Does it use == or .equals(), or is it something else entirely?

I'm mainly interested in the pre 1.7 versions.

like image 578
Raghav Sood Avatar asked Aug 18 '12 16:08

Raghav Sood


People also ask

How does a switch case work?

The switch case in Java works like an if-else ladder, i.e., multiple conditions can be checked at once. Switch is provided with an expression that can be a constant or literal expression that can be evaluated. The value of the expression is matched with each test case till a match is found.

What is the role of switch in the switch case control structure?

A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case.


3 Answers

Neither. it uses the lookupswitch JVM instruction, which is essentially a table lookup. Take a look at the bytecode of the following example:

public static void main(String... args) {
  switch (1) {
  case 1:
    break;
  case 2:
    break;
  }
}

public static void main(java.lang.String[]);
  Code:
   Stack=1, Locals=1, Args_size=1
   0:   iconst_1
   1:   lookupswitch{ //2
                1: 28;
                2: 31;
                default: 31 }
   28:  goto    31
   31:  return
like image 91
João Silva Avatar answered Oct 08 '22 05:10

João Silva


As you can see from this answer, Java switch (at least pre-1.7) does not always compile into == or .equals(). Instead, it uses table lookup. While this is a very small micro-optimization, when doing a great many comparisons, table lookup will almost always be faster.

Note that this is only used for switch statements that check against dense keys. For example, checking an enum value for all of its possibilities would probably yield this primary implementation (internally called tableswitch).

If checking against more sparsely-populated sets of keys, the JVM will use an alternative system, known as lookupswitch. It will instead simply compare various keys and values, doing essentially an optimized == comparison for each possibility. To illustrate these two methods, consider the following two switch statements:

switch (value1) {
case 0:
    a();
    break;
case 1:
    b();
    break;
case 2:
    c();
    break;
case 3:
    d();
    break;
}

switch (value2) {
case 0:
    a();
    break;
case 35:
    b();
    break;
case 103:
    c();
    break;
case 1001:
    d();
    break;
}

The first example would most likely use table lookup, while the other would (basically) use == comparison.

like image 12
Alexis King Avatar answered Oct 08 '22 05:10

Alexis King


Copied from here

In bytecode there are two forms of switch: tableswitch and lookupswitch. One assumes a dense set of keys, the other sparse. See the description of compiling switch in the JVM spec. For enums, the ordinal is found and then the code continues as the int case. I am not entirely sure how the proposed switch on String little feature in JDK7 will be implemented.

However, heavily used code is typically compiled in any sensible JVM. The optimiser is not entirely stupid. Don't worry about it, and follow the usual heuristics for optimisation.

You will find detailed answer over here

like image 7
Amit Deshpande Avatar answered Oct 08 '22 05:10

Amit Deshpande