南山区住房和建设局网站,设计制作我的汽车,河北搭建营销型网站,长安区网站建设目录 前言一、结论二、枚举类型是咋变成 int 类型的#xff1f;三、String 类型是咋变成 int 类型的#xff1f;四、它们的包装类型支持吗#xff1f; 前言
我们知道Java Switch 支持byte、short、int类型#xff0c;在JDK 1.5 时#xff0c;支持了枚举类型#xff0c;在… 目录 前言一、结论二、枚举类型是咋变成 int 类型的三、String 类型是咋变成 int 类型的四、它们的包装类型支持吗 前言
我们知道Java Switch 支持byte、short、int类型在JDK 1.5 时支持了枚举类型在 JDK1.7时又支持了String类型。那么它为什么就不能支持long类型呢明明它跟 byte、short、int 一样都是数值型它又是咋支持 String 类型的呢
一、结论
不卖关子先说结论 switch 底层是使用 int 型 来进行判断的即使是枚举、String类型最终也是转变成 int 型。由于 long 型表示范围大于 int 型因此不支持 long 类型。 下面详细介绍下各个类型是如何被转变成 int 类型的使用的编译命令为 javac反编译网站为http://javare.cn
二、枚举类型是咋变成 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 fieldprivate 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 fieldprivate 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 fieldstatic final int[] $SwitchMap$com$example$express$test$SexEnum;// $FF: synthetic fieldstatic 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 的值。
四、它们的包装类型支持吗
这里以 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毕竟实际还是包装类型的拆箱自然就报空指针了。