How does Java's switch work under the hood?

前端 未结 7 1176
花落未央
花落未央 2020-12-10 23:54

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 ==

相关标签:
7条回答
  • 2020-12-11 00:23

    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.

    0 讨论(0)
  • 2020-12-11 00:25

    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

    0 讨论(0)
  • 2020-12-11 00:30

    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).

    0 讨论(0)
  • 2020-12-11 00:38

    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.

    0 讨论(0)
  • 2020-12-11 00:39

    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.

    0 讨论(0)
  • 2020-12-11 00:41

    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
    
    0 讨论(0)
提交回复
热议问题