switch是怎样支持String的?如何不支持long?

一、结论

不卖关子,先说结论:

switch 底层是使用 int 型 来进行判断的,即使是枚举、String类型,最终也是转变成 int 型。由于 long 型表示范围大于 int 型,因此不支持 long 类型。

下面详细介绍下各个类型是如何被转变成 int 类型的,使用的编译命令为 javac。

二、枚举类型是咋变成 int 类型的?

在没有实验之前,我想当然的认为它是不是根据枚举的 int 型字段来计算的(因为一般枚举都是一个int型,一个string型),但是转念一想,万一枚举没有 int 型字段呢,万一有多个 int 型字段呢,所以肯定不是这样的,下面看实验吧。

定义两个枚举类,一个枚举类有一个int型属性,一个string型属性,另外一个枚举类只有一个string属性:


  1. public enum SexEnum {    
  2.     MALE(1, "男"),    
  3.     FEMALE(0, "女");    
  4.     private int type;  
  5.      private String name;   
  6.      SexEnum(int type, String name) {    
  7.         this.type = type;    
  8.         this.name = name;    
  9.     }    
  10. }    
  11. public enum Sex1Enum {    
  12.     MALE("男"),    
  13.     FEMALE("女");    
  14.     private String name;   
  15.     Sex1Enum(String name) {    
  16.         this.name = name;    
  17.     }    
  18. }   

然后编写一个测试类,并且让两个枚举 switch 的 FEMALE 和 MALE 对应的返回值不同:


  1. public class SwitchTest {    
  2.     public int enumSwitch(SexEnum sex) {    
  3.         switch (sex) {    
  4.             case MALE:    
  5.                 return 1;   
  6.              case FEMALE:  
  7.                  return 2;    
  8.             default:    
  9.                 return 3;    
  10.         }    
  11.     }    
  12.      public int enum1Switch(Sex1Enum sex) { 
  13.          switch (sex) {    
  14.             case FEMALE:    
  15.                 return 1;  
  16.              case MALE:    
  17.                 return 2;  
  18.              default:    
  19.                 return 3;    
  20.         }    
  21.     }    
  22. }   

将这几个类反编译下:


  1. // SexEnum.class    
  2. public enum SexEnum {      
  3.    MALE(1, "鐢�"),    
  4.    FEMALE(0, "濂�");    
  5.    private int type;    
  6.    private String name;    
  7.    // $FF: synthetic field    
  8.    private static final SexEnum[] $VALUES = new SexEnum[]{MALE, FEMALE};     
  9.    private SexEnum(int var3, String var4) {    
  10.       this.type = var3;    
  11.       this.name = var4;    
  12.    }     
  13. }    
  14. // Sex1Enum.class   
  15. public enum Sex1Enum {   
  16.     MALE("鐢�"),  
  17.     FEMALE("濂�"); 
  18.     private String name;    
  19.    // $FF: synthetic field    
  20.    private static final Sex1Enum[] $VALUES = new Sex1Enum[]{MALE, FEMALE};    
  21.     private Sex1Enum(String var3) {    
  22.       this.name = var3;    
  23.    }    
  24. }  

反编译这两个枚举类,发现其中多了一个 $VALUES 数组,内部包含了所有的枚举值。

继续反编译测试类:


  1. // SwitchTest$1.class    
  2. import com.example.express.test.Sex1Enum;    
  3. import com.example.express.test.SexEnum;    
  4. // $FF: synthetic class    
  5. class SwitchTest$1 {      
  6.    // $FF: synthetic field    
  7.    static final int[] $SwitchMap$com$example$express$test$SexEnum;  
  8.     // $FF: synthetic field    
  9.    static final int[] $SwitchMap$com$example$express$test$Sex1Enum = new int[Sex1Enum.values().length];    
  10.    static {    
  11.       try {    
  12.          $SwitchMap$com$example$express$test$Sex1Enum[Sex1Enum.FEMALE.ordinal()] = 1;   
  13.       } catch (NoSuchFieldError var4) {    
  14.          ;    
  15.       }   
  16.        try {    
  17.          $SwitchMap$com$example$express$test$Sex1Enum[Sex1Enum.MALE.ordinal()] = 2;    
  18.       } catch (NoSuchFieldError var3) {    
  19.          ;    
  20.       }   
  21.        $SwitchMap$com$example$express$test$SexEnum = new int[SexEnum.values().length];   
  22.        try {    
  23.          $SwitchMap$com$example$express$test$SexEnum[SexEnum.MALE.ordinal()] = 1;    
  24.       } catch (NoSuchFieldError var2) {    
  25.          ;    
  26.       }    
  27.       try {    
  28.          $SwitchMap$com$example$express$test$SexEnum[SexEnum.FEMALE.ordinal()] = 2;    
  29.       } catch (NoSuchFieldError var1) {    
  30.          ;    
  31.       }    
  32.    }    
  33. }   

首先生成了一个名为 SwitchTest$1.java 的链接类,里面定义了两个枚举数组,这两个数组元素添加的顺序完全和测试类中 switch 类调用的顺序一致。

【声明】:芜湖站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。

相关文章