简体   繁体   中英

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.

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

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.

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

1.Before the arrival of Java 7 , it was "==" , because we could 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 String being an object , ".equals" is used.

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

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

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

If using pre 1.7 Java I assume it uses

==

because for int you can't do equals for example and in case of enum, equals and == will return the same

EDIT

My assumption is wrong it uses a lookuptable, in bytecode it would like like:

 tableswitch

If you are using primitive types, such as integers, then java will use == to compare them If you are using Strings, then java will use the equals() method to test if the strings are equal. If you are using a switch statement with enums, then == and equals() are both the same, so it doesn't really matter which one is being used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM