其他
Java 中的 Switch 都支持 String 了,为什么不支持 long?
1、《往期精选优秀博文都在这里了!》 2、CTO 说了,用错 @Autowired 和 @Resource 的人可以领盒饭了 3、程序员离职事件始末 4、如何解决MySQL order by limit语句的分页数据重复问题? 5、Java中八个潜在的内存泄露风险,你知道几个?
1
结论
2
枚举类型是咋变成 int 类型的?
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;
}
}
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;
}
}
}
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;
}
}
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) {
;
}
}
}
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;
}
}
}
3
String 类型是咋变成 int 类型的?
switch (c) {
case 'a':
return 1;
case 'b':
return 2;
default:
return Integer.MAX_VALUE;
}
}
switch(var1) {
case 97:
return 1;
case 98:
return 2;
default:
return Integer.MAX_VALUE;
}
}
switch (ss) {
case "ABCDEa123abc":
return 1;
case "ABCDFB123abc":
return 2;
case "helloWorld":
return 3;
default:
return Integer.MAX_VALUE;
}
}
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;
}
}
4
它们的包装类型支持吗?
switch (c) {
case 1:
return 1;
case 2:
return 2;
}
return -1;
}
switch(var1.intValue()) {
case 1:
return 1;
case 2:
return 2;
default:
return -1;
}
}