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

          共 6857字,需瀏覽 14分鐘

           ·

          2021-01-28 11:36

          往期熱門(mén)文章:
          1、數(shù)據(jù)庫(kù)中寫(xiě)放大問(wèn)題,大牛居然用“透明壓縮技術(shù)”緩解了
          2、為什么要放棄 JSP ?
          3、請(qǐng)謹(jǐn)慎使用Arrays.asList、ArrayList的subList
          4、人臉識(shí)別“抓”錯(cuò)了人,他在監(jiān)獄呆了 10 天
          5、這四種情況下,才是考慮分庫(kù)分表的時(shí)候!

          來(lái)源 |?jitwxs.cn/6f3eddff.html

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

          一、結(jié)論

          不賣(mài)關(guān)子,先說(shuō)結(jié)論:

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

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

          二、枚舉類(lèi)型是咋變成 int 類(lèi)型的?

          在沒(méi)有實(shí)驗(yàn)之前,我想當(dāng)然的認(rèn)為它是不是根據(jù)枚舉的 int 型字段來(lái)計(jì)算的(因?yàn)橐话忝杜e都是一個(gè)int型,一個(gè)string型),但是轉(zhuǎn)念一想,萬(wàn)一枚舉沒(méi)有 int 型字段呢,萬(wàn)一有多個(gè) int 型字段呢,所以肯定不是這樣的,下面看實(shí)驗(yàn)吧。

          定義兩個(gè)枚舉類(lèi),一個(gè)枚舉類(lèi)有一個(gè)int型屬性,一個(gè)string型屬性,另外一個(gè)枚舉類(lèi)只有一個(gè)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;??
          ????}??
          }??

          然后編寫(xiě)一個(gè)測(cè)試類(lèi),并且讓兩個(gè)枚舉 switch 的 FEMALE 和 MALE 對(duì)應(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;??
          ????????}??
          ????}??
          }??

          將這幾個(gè)類(lèi)反編譯下:

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

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

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

          首先生成了一個(gè)名為 SwitchTest$1.java 的鏈接類(lèi),里面定義了兩個(gè)枚舉數(shù)組,這兩個(gè)數(shù)組元素添加的順序完全和測(cè)試類(lèi)中 switch 類(lèi)調(diào)用的順序一致。

          圖片

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

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

          //?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 類(lèi)型是咋變成 int 類(lèi)型的?

          首先我們先知道 char 類(lèi)型是如何變成 int 類(lèi)型的,很簡(jiǎn)單,是 ASCII 碼,例如存在 switch 語(yǔ)句:

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

          那么對(duì)于 String 來(lái)說(shuō),利用的就是 hashCode() 函數(shù)了,但是 兩個(gè)不同的字符串 hashCode() 是有可能相等的,這時(shí)候就得靠 equals() 函數(shù)了,例如存在 switch 語(yǔ)句:

          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,對(duì)于 hashCode 相等情況通過(guò) equals() 方法判斷,最后再判斷 var3 的值。

          四、它們的包裝類(lèi)型支持嗎?

          這里以 Integer 類(lèi)型為例,Character 和 Byte 同理,例如存在 switch 語(yǔ)句:

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

          可以看到,是支持包裝類(lèi)型的,通過(guò)自動(dòng)拆箱解決。

          那萬(wàn)一包裝類(lèi)型是 NULL 咋辦,首先我們知道 swtich 的 case 是不給加 null 的,編譯都通不過(guò),那如果傳 null 呢?

          答案是 NPE,畢竟實(shí)際還是包裝類(lèi)型的拆箱,自然就報(bào)空指針了。


          最近熱文閱讀:

          1、數(shù)據(jù)庫(kù)中寫(xiě)放大問(wèn)題,大牛居然用“透明壓縮技術(shù)”緩解了
          2、為什么要放棄 JSP ?
          3、請(qǐng)謹(jǐn)慎使用Arrays.asList、ArrayList的subList
          4、人臉識(shí)別“抓”錯(cuò)了人,他在監(jiān)獄呆了 10 天
          5、騷操作 !IDEA 防止寫(xiě)代碼沉迷插件 !
          6、這四種情況下,才是考慮分庫(kù)分表的時(shí)候!
          7、求求你別再用offset和limit分頁(yè)了
          8、10 個(gè)最好用的重構(gòu)小技巧排行榜,你用過(guò)哪些?
          9、淺談?dòng)貌缓镁彺娴膸讉€(gè)受傷場(chǎng)景!
          10、高并發(fā)下接口冪等性解決方案
          ?關(guān)注公眾號(hào),你想要的Java都在這里

          瀏覽 70
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <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>
                  日本韩国无码 | 欧美成人在线观看免费 | 激情乱伦文学 | 韩国一区二区三区在线 | 乱伦小说av |