- 注意【break】的使用
package struct; public class Demo06 { public static void main(String[] args) { char grade = 'C'; //case穿透现象,需要加break //switch匹配一个具体的值 switch (grade){ case 'A': System.out.println("优秀!"); case 'B': System.out.println("良好!"); case 'C': System.out.println("合格!"); case 'D': System.out.println("不合格!"); default: System.out.println("未知等级!"); } } }
package struct; public class Demo06 { public static void main(String[] args) { char grade = 'C'; //case穿透现象,需要加break //switch匹配一个具体的值 switch (grade){ case 'A': System.out.println("优秀!"); break; case 'B': System.out.println("良好!"); break; case 'C': System.out.println("合格!"); case 'D': System.out.println("不合格!"); break; default: System.out.println("未知等级!"); } } }switch支持字符串String类型了(JDK7新特性)
package struct; public class Demo07 { public static void main(String[] args) { String name = "IT_Jay"; //JDK的新特性,表达式结果可以是字符串!! //字符的本质还是数字 //反编译 java--class(字节码文件) IDEA可实现反编译 switch (name){ case "帅小伙": System.out.println("帅小伙"); break; case "IT_Jay": System.out.println("IT_Jay"); break; default: System.out.println("未匹配到!"); } } }IDEA实现反编译
- 打开项目结构【File】菜单下【Project Structure】
- CV框中路径,在文件管理器中打开
- 打开发现为乱码,直接拖拽(剪切)到IDEA是会被拒绝的
- 回到IDEA,可以看到【Demo07.class】已经在包【struct】下了,双击打开,即可查看源码
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by FernFlower decompiler) // package struct; public class Demo07 { public Demo07() { } public static void main(String[] args) { String name = "IT_Jay"; byte var3 = -1; switch(name.hashCode()) { case -2124559122: if (name.equals("IT_Jay")) { var3 = 1; } break; case 23881135: if (name.equals("帅小伙")) { var3 = 0; } } switch(var3) { case 0: System.out.println("帅小伙"); break; case 1: System.out.println("IT_Jay"); break; default: System.out.println("为匹配到!"); } } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)