问题诞生 在一次代码review中,被老板喷了下为啥用switch而不用if;对此有点疑惑,当时的理由是:写switch会遗漏break,导致代码出现异常逻辑;当使用switch时,若break漏写的确会导致代码逻辑错误;但这理由并不充分,那么switch到底有啥优点呢?
下面我们探寻下switch的原理!
Switch用法 二话不说,先上代码:以java为例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 int type = 1 ;switch (type) { case 1 : System.out.println("1" ); break ; case 2 : System.out.println("2" ); break ; case 3 : System.out.println("3" ); break ; case 4 : System.out.println("4" ); break ; case 5 : System.out.println("5" ); break ; default : System.out.println("default" ); break ; }
switch用于条件选择,分支逻辑;switch接受基本类型:int,char,enum,short等,此外还支持String类型。
先查看下上述代码的字节码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 Code: stack=2 , locals=2 , args_size=1 0 : iconst_4 1 : istore_1 2 : iload_1 3 : tableswitch { 1 : 36 2 : 47 3 : 58 4 : 69 5 : 80 default : 91 } 36: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream; 39: ldc #3 // String 1 41: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 44 : goto 99 47: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream; 50: ldc #5 // String 2 52: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 55 : goto 99 58: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream; 61: ldc #6 // String 3 63: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 66 : goto 99 69: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream; 72: ldc #7 // String 4 74: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 77 : goto 99 80: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream; 83: ldc #8 // String 5 85: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 88 : goto 99 91: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream; 94: ldc #9 // String default 96: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 99 : return LineNumberTable: line 9 : 0 line 10 : 2 line 12 : 36 line 13 : 44 line 15 : 47 line 16 : 55 line 18 : 58 line 19 : 66 line 21 : 69 line 22 : 77 line 24 : 80 line 25 : 88 line 27 : 91 line 30 : 99
从上述字节码可以看出,int类型的switch变成了tableswitch
的一张表;运行时,直接从表中查找跳转地址,查询时间O(1)。
下面再看看String类型的switch代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 String type = "love" ; switch (type) { case "the" : System.out.println("1" ); break ; case "auto" : System.out.println("2" ); break ; case "hello" : System.out.println("3" ); break ; case "world" : System.out.println("4" ); break ; case "love" : System.out.println("5" ); break ; default : System.out.println("default" ); break ; }
字符串类型的switch的字节码又是咋样的呢?如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 Code: stack=2 , locals=4 , args_size=1 0: ldc #2 // String love 2 : astore_1 3 : aload_1 4 : astore_2 5 : iconst_m1 6 : istore_3 7 : aload_2 8: invokevirtual #3 // Method java/lang/String.hashCode:()I 11 : lookupswitch { 114801 : 60 3005871 : 74 3327858 : 116 99162322 : 88 113318802 : 102 default : 127 } 60 : aload_2 61: ldc #4 // String the 63: invokevirtual #5 // Method java/lang/String.equals:(Ljava/lang/Object;)Z 66 : ifeq 127 69 : iconst_0 70 : istore_3 71 : goto 127 74 : aload_2 75: ldc #6 // String auto 77: invokevirtual #5 // Method java/lang/String.equals:(Ljava/lang/Object;)Z 80 : ifeq 127 83 : iconst_1 84 : istore_3 85 : goto 127 88 : aload_2 89: ldc #7 // String hello 91: invokevirtual #5 // Method java/lang/String.equals:(Ljava/lang/Object;)Z 94 : ifeq 127 97 : iconst_2 98 : istore_3 99 : goto 127 102 : aload_2 103: ldc #8 // String world 105: invokevirtual #5 // Method java/lang/String.equals:(Ljava/lang/Object;)Z 108 : ifeq 127 111 : iconst_3 112 : istore_3 113 : goto 127 116 : aload_2 117: ldc #2 // String love 119: invokevirtual #5 // Method java/lang/String.equals:(Ljava/lang/Object;)Z 122 : ifeq 127 125 : iconst_4 126 : istore_3 127 : iload_3 128 : tableswitch { 0 : 164 1 : 175 2 : 186 3 : 197 4 : 208 default : 219 } 164: getstatic #9 // Field java/lang/System.out:Ljava/io/PrintStream; 167: ldc #10 // String 1 169: invokevirtual #11 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 172 : goto 227 175: getstatic #9 // Field java/lang/System.out:Ljava/io/PrintStream; 178: ldc #12 // String 2 180: invokevirtual #11 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 183 : goto 227 186: getstatic #9 // Field java/lang/System.out:Ljava/io/PrintStream; 189: ldc #13 // String 3 191: invokevirtual #11 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 194 : goto 227 197: getstatic #9 // Field java/lang/System.out:Ljava/io/PrintStream; 200: ldc #14 // String 4 202: invokevirtual #11 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 205 : goto 227 208: getstatic #9 // Field java/lang/System.out:Ljava/io/PrintStream; 211: ldc #15 // String 5 213: invokevirtual #11 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 216 : goto 227 219: getstatic #9 // Field java/lang/System.out:Ljava/io/PrintStream; 222: ldc #16 // String default 224: invokevirtual #11 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 227 : return LineNumberTable: line 9 : 0 line 10 : 3 line 12 : 164 line 13 : 172 line 15 : 175 line 16 : 183 line 18 : 186 line 19 : 194 line 21 : 197 line 22 : 205 line 24 : 208 line 25 : 216 line 27 : 219 line 30 : 227
从上面看出来,String的switch使用了tableswitch
和lookupswitch
两种表;我们来分析下这段字节码,其实做了下面几件事:
JVM先创建tableswitch,用索引来表示真正执行的代码;
然后根据String的hashCode生成lookupswitch,key为hashCode,value为偏移量,主要用于跳转到tableswitch;
当一个String进入时,会在lookup中进行比较,时间复杂度为O(n);然后tableswitch的时间复杂度为O(1);整体的复杂度为O(n)。
那什么时候用tableswitch,什么时候用lookupswitch呢???
首先看下官方的介绍:
Where the cases of the switch are sparse, the table representation of the tableswitch instruction becomes inefficient in terms of space. The lookupswitch instruction may be used instead. The lookupswitch instruction pairs int keys (the values of the case labels) with target offsets in a table. When a lookupswitch instruction is executed, the value of the expression of the switch is compared against the keys in the table. If one of the keys matches the value of the expression, execution continues at the associated target offset. If no key matches, execution continues at the default target.
用中文翻译下就是:
当switch中case的值可以被表示为一个表中的索引值时,则使用tableswitch,比如:1,2,3,4这样的;当case的值非常稀疏,那用tableswitch做索引值时,会耗费大量的空间;所以lookupswitch的做法是:把case的int值和偏移量作为一对放在一个表里。
switch的问题解决了,还有一个问题,它比if有没有啥优点???
先上if的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 int type = 5 ;if (type == 1 ) { System.out.println("1" ); } else if (type == 2 ) { System.out.println("2" ); } else if (type == 3 ) { System.out.println("3" ); } else if (type == 4 ) { System.out.println("4" ); } else if (type == 5 ) { System.out.println("5" ); } else { System.out.println("default" ); }
java字节码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 Code: stack=2 , locals=2 , args_size=1 0 : iconst_5 1 : istore_1 2 : iload_1 3 : iconst_1 4 : if_icmpne 18 7: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream; 10: ldc #3 // String 1 12: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 15 : goto 90 18 : iload_1 19 : iconst_2 20 : if_icmpne 34 23: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream; 26: ldc #5 // String 2 28: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 31 : goto 90 34 : iload_1 35 : iconst_3 36 : if_icmpne 50 39: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream; 42: ldc #6 // String 3 44: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 47 : goto 90 50 : iload_1 51 : iconst_4 52 : if_icmpne 66 55: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream; 58: ldc #7 // String 4 60: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 63 : goto 90 66 : iload_1 67 : iconst_5 68 : if_icmpne 82 71: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream; 74: ldc #8 // String 5 76: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 79 : goto 90 82: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream; 85: ldc #9 // String default 87: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 90 : return LineNumberTable: line 9 : 0 line 10 : 2 line 11 : 7 line 12 : 18 line 13 : 23 line 14 : 34 line 15 : 39 line 16 : 50 line 17 : 55 line 18 : 66 line 19 : 71 line 21 : 82 line 23 : 90
相比于相应的switch代码,if代码每次都会加载下条件,而switch只是第一次加载条件;因此,理论上switch比if快。
后记 本文还有几个待解决的问题:
(1)tableswitch和lookupswitch内部如何实现的?跳表,需要深入研究下;
(2)当代码中条件特别多的时候,代码会出现大量的分支,应该如何减少if或者switch代码?
参考 [1] Java字节码:http://gityuan.com/2015/10/24/jvm-bytecode-grammar/