if(条件表达式){
语句序列1;
}
else {
语句序列2;
}
if(条件表达式1){
语句序列1;
}
else if(条件表达式2) {
语句序列2;
}
else if(条件表达式3) {
语句序列3;
}
......
else {
语句序列n+1;
}
if(条件表达式1){
if(条件表达式1){
}
}
注意:
switch(表达式){
case 常量表达式1:
语句序列1;
break;
case 常量表达式2:
语句序列2;
break;
.........
case 常量表达式n:
语句序列n;
break;
default:
语句序列n+1;
}
package com.study.struct;
public class switch2 {
public static void main(String[] args) {
//jdk7新特性,表达式结果可以是字符串!!
//字符的本质还是数字
String name = "大橙子";
switch(name){
case "张三":
System.out.println("张三");
break; //防止case穿透
case "大橙子":
System.out.println("大橙子");
break;
case "李四":
System.out.println("李四");
break;
default:
System.out.println("干啥了");
}
}
}
idea 反编译
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by FernFlower decompiler) // package com.study.struct; public class switch2 { public switch2() { } public static void main(String[] args) { String name = "大橙子"; byte var3 = -1; switch(name.hashCode()) { case 774889: if (name.equals("张三")) { var3 = 0; } break; case 842061: if (name.equals("李四")) { var3 = 2; } break; case 22800254: if (name.equals("大橙子")) { var3 = 1; } } switch(var3) { case 0: System.out.println("张三"); break; case 1: System.out.println("大橙子"); break; case 2: System.out.println("李四"); break; default: System.out.println("干啥了"); } } }
原文:https://www.cnblogs.com/chengmengdui/p/14726347.html