在Switch中可以使用的类型有枚举、字符串类型与整形int类型,下面来具体看这几个类型。
1、switch为枚举类型
枚举类:
enum Fruit { APPLE,ORINGE }
调用javac生成的代码如下:
enum Fruit{ private <init>(/*synthetic*/ String $enum$name, /*synthetic*/ int $enum$ordinal) { super($enum$name, $enum$ordinal); } /*public static final*/ APPLE /* = new Fruit("APPLE", 0) */ /*public static final*/ ORINGE /* = new Fruit("ORINGE", 1) */ /*synthetic*/ private static final Fruit[] $VALUES = new Fruit[]{Fruit.APPLE, Fruit.ORINGE} public static Fruit[] values() { return (Fruit[])$VALUES.clone(); } public static Fruit valueOf(String name) { return (Fruit)Enum.valueOf(Fruit.class, name); } }
Enum在Java中也被当作一个类来处理,并且Enum也算是Java中的语法糖,主要通过Javac中的Lower类来处理枚举这个语法糖的。
查看javac的英文说明,如下:
This map gives a translation table to be used for enum switches.
For each enum that appears as the type of a switch expression, we maintain an EnumMapping(举例) to assist in the translation, as exemplified by the following example:
we translate
Fruit fruit = Fruit.APPLE; switch (fruit) { case APPLE: System.out.println("apple"); break; case ORANGE: System.out.println("orange"); break; default: System.out.println("unknow"); }
into
switch (com.test19.TestEnumClass$1.$SwitchMap$com$test19$Fruit[(fruit).ordinal()]) { case 1: System.out.println("apple"); break; case 2: System.out.println("orange"); break; default: System.out.println("unknow"); }
with the auxiliary table initialized as follows:
/*synthetic*/ class TestEnumClass$1 { /*synthetic*/ static final int[] $SwitchMap$com$test19$Fruit = new int[Fruit.values().length]; static { try { com.test19.TestEnumClass$1.$SwitchMap$com$test19$Fruit[Fruit.APPLE.ordinal()] = 1; } catch (NoSuchFieldError ex) { } try { com.test19.TestEnumClass$1.$SwitchMap$com$test19$Fruit[Fruit.ORANGE.ordinal()] = 2; } catch (NoSuchFieldError ex) { } } }
class EnumMapping provides mapping data and support methods for this translation.
2、switch为字符串类型
下面继续来看switch对于字符串的处理。
The general approach used is to translate a single string switch statement into a series of two chained switch statements:
使用的一般方法是将一个字符串类型的switch语句翻译为两个switch语句:
the first a synthesized statement switching on the argument string's hash value and
computing a string's position in the list of original case labels, if any, followed by a second switch on the
computed integer value.
The second switch has the same code structure as the original string switch statement
except that the string case labels are replaced with positional integer constants starting at 0.
The first switch statement can be thought of as an inlined map from strings to their position in the case
label list. An alternate implementation would use an actual Map for this purpose, as done for enum switches.
With some additional effort, it would be possible to use a single switch statement on the hash code of the
argument, but care would need to be taken to preserve the proper control flow in the presence of hash
collisions and other complications, such as fallthroughs. Switch statements with one or two
alternatives could also be specially translated into if-then statements to omit the computation of the hash
code.
The generated code assumes that the hashing algorithm of String is the same in the compilation environment as
in the environment the code will run in. The string hashing algorithm in the SE JDK has been unchanged
since at least JDK 1.2. Since the algorithm has been specified since that release as well, it is very
unlikely to be changed in the future.
Different hashing algorithms, such as the length of the strings or a perfect hashing algorithm over the
particular set of case labels, could potentially be used instead of String.hashCode.
举一个具体的例子,如下:
class TestStringSwitch { public void test() { String fruit = ""; switch (fruit) { case "banana": case "apple": System.out.println("banana or orange"); break; case "orange": System.out.println("orange"); break; default: System.out.println("default"); break; } } }
翻译后的最终结果为:
/*synthetic*/ final String s99$ = (fruit); /*synthetic*/ int tmp99$ = -1; switch (s99$.hashCode()) { case -1396355227: if (s99$.equals("banana")) tmp99$ = 0; break; case 93029210: if (s99$.equals("apple")) tmp99$ = 1; break; case -1008851410: if (s99$.equals("orange")) tmp99$ = 2; break; } switch (tmp99$) { case 0: case 1: System.out.println("banana or orange"); break; case 2: System.out.println("orange"); break; default: System.out.println("default"); break; }
哈希函数在映射的时候可能存在冲突,多个字符串的哈希值可能是一样的,所以还要通过字符串比较来保证。