switch是如何支持String的?為什么不支持long?
我們知道 Java Switch 支持byte、short、int 類型,在 JDK 1.5 時,支持了枚舉類型,在 JDK 1.7 時,又支持了 String類型。
那么它為什么就不能支持 long 類型呢,明明它跟 byte、short、int 一樣都是數值型,它又是咋支持 String 類型的呢?
一、結論
不賣關子,先說結論:
switch 底層是使用 int 型 來進行判斷的,即使是枚舉、String類型,最終也是轉變成 int 型。由于 long 型表示范圍大于 int 型,因此不支持 long 類型。
下面詳細介紹下各個類型是如何被轉變成 int 類型的,使用的編譯命令為 javac。
二、枚舉類型是咋變成 int 類型的?
在沒有實驗之前,我想當然的認為它是不是根據枚舉的 int 型字段來計算的(因為一般枚舉都是一個int型,一個string型),但是轉念一想,萬一枚舉沒有 int 型字段呢,萬一有多個 int 型字段呢,所以肯定不是這樣的,下面看實驗吧。
定義兩個枚舉類,一個枚舉類有一個int型屬性,一個string型屬性,另外一個枚舉類只有一個string屬性:
- public enum SexEnum {
- MALE(1, "男"),
- FEMALE(0, "女");
- private int type;
- private String name;
- SexEnum(int type, String name) {
- this.type = type;
- this.name = name;
- }
- }
- public enum Sex1Enum {
- MALE("男"),
- FEMALE("女");
- private String name;
- Sex1Enum(String name) {
- this.name = name;
- }
- }
然后編寫一個測試類,并且讓兩個枚舉 switch 的 FEMALE 和 MALE 對應的返回值不同:
- public class SwitchTest {
- public int enumSwitch(SexEnum sex) {
- switch (sex) {
- case MALE:
- return 1;
- case FEMALE:
- return 2;
- default:
- return 3;
- }
- }
- public int enum1Switch(Sex1Enum sex) {
- switch (sex) {
- case FEMALE:
- return 1;
- case MALE:
- return 2;
- default:
- return 3;
- }
- }
- }
將這幾個類反編譯下:
- // SexEnum.class
- public enum SexEnum {
- MALE(1, "鐢�"),
- FEMALE(0, "濂�");
- private int type;
- private String name;
- // $FF: synthetic field
- private static final SexEnum[] $VALUES = new SexEnum[]{MALE, FEMALE};
- private SexEnum(int var3, String var4) {
- this.type = var3;
- this.name = var4;
- }
- }
- // Sex1Enum.class
- public enum Sex1Enum {
- MALE("鐢�"),
- FEMALE("濂�");
- private String name;
- // $FF: synthetic field
- private static final Sex1Enum[] $VALUES = new Sex1Enum[]{MALE, FEMALE};
- private Sex1Enum(String var3) {
- this.name = var3;
- }
- }
反編譯這兩個枚舉類,發現其中多了一個 $VALUES 數組,內部包含了所有的枚舉值。
繼續反編譯測試類:
- // SwitchTest$1.class
- import com.example.express.test.Sex1Enum;
- import com.example.express.test.SexEnum;
- // $FF: synthetic class
- class SwitchTest$1 {
- // $FF: synthetic field
- static final int[] $SwitchMap$com$example$express$test$SexEnum;
- // $FF: synthetic field
- static final int[] $SwitchMap$com$example$express$test$Sex1Enum = new int[Sex1Enum.values().length];
- static {
- try {
- $SwitchMap$com$example$express$test$Sex1Enum[Sex1Enum.FEMALE.ordinal()] = 1;
- } catch (NoSuchFieldError var4) {
- ;
- }
- try {
- $SwitchMap$com$example$express$test$Sex1Enum[Sex1Enum.MALE.ordinal()] = 2;
- } catch (NoSuchFieldError var3) {
- ;
- }
- $SwitchMap$com$example$express$test$SexEnum = new int[SexEnum.values().length];
- try {
- $SwitchMap$com$example$express$test$SexEnum[SexEnum.MALE.ordinal()] = 1;
- } catch (NoSuchFieldError var2) {
- ;
- }
- try {
- $SwitchMap$com$example$express$test$SexEnum[SexEnum.FEMALE.ordinal()] = 2;
- } catch (NoSuchFieldError var1) {
- ;
- }
- }
- }
首先生成了一個名為 SwitchTest$1.java 的鏈接類,里面定義了兩個枚舉數組,這兩個數組元素添加的順序完全和測試類中 switch 類調用的順序一致。
枚舉元素在數組中的下標由 ordinal() 函數決定,該方法就是返回枚舉元素在枚舉類中的序號。
這里我們其實就已經知道了,在 switch 語句中,是根據枚舉元素在枚舉中的序號來轉變成 int 型的。最后再看下測試類的反編譯結果驗證下:
- // SwitchTest.class
- import com.example.express.test.Sex1Enum;
- import com.example.express.test.SexEnum;
- import com.example.express.test.SwitchTest.1;
- public class SwitchTest {
- public int enumSwitch(SexEnum var1) {
- switch(1.$SwitchMap$com$example$express$test$SexEnum[var1.ordinal()]) {
- case 1:
- return 1;
- case 2:
- return 2;
- default:
- return 3;
- }
- }
- public int enum1Switch(Sex1Enum var1) {
- switch(1.$SwitchMap$com$example$express$test$Sex1Enum[var1.ordinal()]) {
- case 1:
- return 1;
- case 2:
- return 2;
- default:
- return 3;
- }
- }
- }
三、String 類型是咋變成 int 類型的?
首先我們先知道 char 類型是如何變成 int 類型的,很簡單,是 ASCII 碼,例如存在 switch 語句:
- public int charSwitch(char c) {
- switch (c) {
- case 'a':
- return 1;
- case 'b':
- return 2;
- default:
- return Integer.MAX_VALUE;
- }
- }
反編譯結果:
- public int charSwitch(char var1) {
- switch(var1) {
- case 97:
- return 1;
- case 98:
- return 2;
- default:
- return Integer.MAX_VALUE;
- }
- }
那么對于 String 來說,利用的就是 hashCode() 函數了,但是 兩個不同的字符串 hashCode() 是有可能相等的,這時候就得靠 equals() 函數了,例如存在 switch 語句:
- public int stringSwitch(String ss) {
- switch (ss) {
- case "ABCDEa123abc":
- return 1;
- case "ABCDFB123abc":
- return 2;
- case "helloWorld":
- return 3;
- default:
- return Integer.MAX_VALUE;
- }
- }
其中字符串 ABCDEa123abc 和 ABCDFB123abc 的 hashCode 是相等的,反編譯結果為:
- public int stringSwitch(String var1) {
- byte var3 = -1;
- switch(var1.hashCode()) {
- case -1554135584:
- if(var1.equals("helloWorld")) {
- var3 = 2;
- }
- break;
- case 165374702:
- if(var1.equals("ABCDFB123abc")) {
- var3 = 1;
- } else if(var1.equals("ABCDEa123abc")) {
- var3 = 0;
- }
- }
- switch(var3) {
- case 0:
- return 1;
- case 1:
- return 2;
- case 2:
- return 3;
- default:
- return Integer.MAX_VALUE;
- }
- }
可以看到它引入了局部變量 var3,對于 hashCode 相等情況通過 equals() 方法判斷,最后再判斷 var3 的值。另外,關注公眾號Java技術棧,在后臺回復:面試,可以獲取我整理的 Java 系列面試題和答案,非常齊全。
四、它們的包裝類型支持嗎?
這里以 Integer 類型為例,Character 和 Byte 同理,例如存在 switch 語句:
- public int integerSwitch(Integer c) {
- switch (c) {
- case 1:
- return 1;
- case 2:
- return 2;
- }
- return -1;
- }
反編譯結果為:
- public int integerSwitch(Integer var1) {
- switch(var1.intValue()) {
- case 1:
- return 1;
- case 2:
- return 2;
- default:
- return -1;
- }
- }
可以看到,是支持包裝類型的,通過自動拆箱解決。
那萬一包裝類型是 NULL 咋辦,首先我們知道 swtich 的 case 是不給加 null 的,編譯都通不過,那如果傳 null 呢?
答案是 NPE,畢竟實際還是包裝類型的拆箱,自然就報空指針了。
另外,關注公眾號Java技術棧,在后臺回復:面試,可以獲取我整理的 Java 系列面試題和答案,非常齊全。