[ACCEPTED]-How does Java's switch work under the hood?-switch-statement

Accepted answer
Score: 21

Neither. it uses the lookupswitch JVM instruction, which is 2 essentially a table lookup. Take a look 1 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
Score: 12

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

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

If checking against 9 more sparsely-populated sets of keys, the 8 JVM will use an alternative system, known 7 as lookupswitch. It will instead simply compare various 6 keys and values, doing essentially an optimized 5 == comparison for each possibility. To illustrate 4 these two methods, consider the following 3 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 2 would most likely use table lookup, while 1 the other would (basically) use == comparison.

Score: 7

Copied from here

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

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

You will find detailed 1 answer over here

Score: 1

1.Before the arrival of Java 7, it was "==", because we could 4 use the integer and char for switch case, and as they were primitive, so it had to be "==".

2. From Java 7, String also was allowed in switch case, and 3 String being an object, ".equals" is used.

I would like 2 to add this... that "==" is used to compare the Object Reference Variable, not the Object itself. Using 1 ".equals" we compare the objects.

Score: 0

A switch works with the byte, short, char, and 8 int primitive data types. It also works 7 with enumerated types (discussed in Enum 6 Types), the String class, and a few special 5 classes that wrap certain primitive types: Character, Byte, Short, and 4 Integer. (Java 1.6)

While primitives are 3 compared with ==, the switch method surely uses 2 this kind of comparison in Java 1.6 (and 1 earlier).

More Related questions