<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Java中的 Switch 是如何支持 String 的?為什么不支持 long?

          共 6779字,需瀏覽 14分鐘

           ·

          2021-01-25 01:37

          點(diǎn)擊上方藍(lán)色“小哈學(xué)Java”,選擇“設(shè)為星標(biāo)

          回復(fù)“資源”獲取獨(dú)家整理的學(xué)習(xí)資料!

          作者:jitwxs

          https://jitwxs.cn/6f3eddff.html

          我們知道 Java Switch 支持byte、short、int 類型,在 JDK 1.5 時,支持了枚舉類型,在 JDK 1.7 時,又支持了 String類型。那么它為什么就不能支持 long 類型呢,明明它跟 byte、short、int 一樣都是數(shù)值型,它又是咋支持 String 類型的呢?

          一、結(jié)論

          不賣關(guān)子,先說結(jié)論:

          switch 底層是使用 int 型 來進(jìn)行判斷的,即使是枚舉、String類型,最終也是轉(zhuǎn)變成 int 型。由于 long 型表示范圍大于 int 型,因此不支持 long 類型。

          下面詳細(xì)介紹下各個類型是如何被轉(zhuǎn)變成 int 類型的,使用的編譯命令為 javac,反編譯網(wǎng)站為:http://javare.cn

          二、枚舉類型是咋變成 int 類型的?

          在沒有實(shí)驗之前,我想當(dāng)然的認(rèn)為它是不是根據(jù)枚舉的 int 型字段來計算的(因為一般枚舉都是一個int型,一個string型),但是轉(zhuǎn)念一想,萬一枚舉沒有 int 型字段呢,萬一有多個 int 型字段呢,所以肯定不是這樣的,下面看實(shí)驗吧。

          定義兩個枚舉類,一個枚舉類有一個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 對應(yīng)的返回值不同:

          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;??
          ???}??
          ??
          }??

          反編譯這兩個枚舉類,發(fā)現(xiàn)其中多了一個 $VALUES 數(shù)組,內(nèi)部包含了所有的枚舉值。繼續(xù)反編譯測試類:

          //?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 的鏈接類,里面定義了兩個枚舉數(shù)組,這兩個數(shù)組元素添加的順序完全和測試類中 switch 類調(diào)用的順序一致。

          圖片

          枚舉元素在數(shù)組中的下標(biāo)由 ordinal() 函數(shù)決定,該方法就是返回枚舉元素在枚舉類中的序號。

          這里我們其實(shí)就已經(jīng)知道了,在 switch 語句中,是根據(jù)枚舉元素在枚舉中的序號來轉(zhuǎn)變成 int 型的。最后再看下測試類的反編譯結(jié)果驗證下:

          //?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;??
          ????}??
          }??

          反編譯結(jié)果:

          public?int?charSwitch(char?var1)?{??
          ????switch(var1)?{??
          ????????case?97:??
          ????????????return?1;??
          ????????case?98:??
          ????????????return?2;??
          ????????default:??
          ????????????return?Integer.MAX_VALUE;??
          ????}??
          }??

          那么對于 String 來說,利用的就是 hashCode() 函數(shù)了,但是 兩個不同的字符串 hashCode() 是有可能相等的,這時候就得靠 equals() 函數(shù)了,例如存在 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 是相等的,反編譯結(jié)果為:

          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 的值。

          四、它們的包裝類型支持嗎?

          這里以 Integer 類型為例,Character 和 Byte 同理,例如存在 switch 語句:

          public?int?integerSwitch(Integer?c)?{??
          ????switch?(c)?{??
          ????????case?1:??
          ????????????return?1;??
          ????????case?2:??
          ????????????return?2;??
          ????}??
          ????return?-1;??
          }??

          反編譯結(jié)果為:

          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,畢竟實(shí)際還是包裝類型的拆箱,自然就報空指針了。

          圖片

          END


          有熱門推薦??

          1.?API接口的安全設(shè)計驗證:ticket,簽名,時間戳

          2.?服務(wù)的心跳機(jī)制與斷線重連,Netty底層是怎么實(shí)現(xiàn)的?

          3.?面試官:Java 反射是什么?我回答不上來!

          4.?一個員工的離職成本到底有多恐怖!

          最近面試BAT,整理一份面試資料Java面試BATJ通關(guān)手冊,覆蓋了Java核心技術(shù)、JVM、Java并發(fā)、SSM、微服務(wù)、數(shù)據(jù)庫、數(shù)據(jù)結(jié)構(gòu)等等。

          獲取方式:點(diǎn)“在看”,關(guān)注公眾號并回復(fù)?Java?領(lǐng)取,更多內(nèi)容陸續(xù)奉上。

          文章有幫助的話,在看,轉(zhuǎn)發(fā)吧。

          謝謝支持喲 (*^__^*)

          瀏覽 68
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  亚洲色无码专区观看在线观 | 超碰乱| 一道本中文字幕欧美激情 | 日本成人性爱视频网站 | 亚洲免费看片 |