Switch
package com.zishi.struct; public class SwitchDemo01 { public static void main(String[] args) { //case穿透 //switch 匹配一个具体的值 char grade = 'D'; switch (grade){ case 'A': System.out.println("优秀"); break;//可选 case 'B': System.out.println("良好"); break; case 'C': System.out.println("中等"); break; case 'D': System.out.println("及格"); case 'E': System.out.println("不及格"); default: System.out.println("成绩未知"); } } }
package com.zishi.struct; public class SwitchDemo02 { public static void main(String[] args) { String name = "一只肥"; //JDK7新特性,表达式结果可以是字符串 //字符串的本质还是数字 //反编译 java----class(字节码文件)-----反编译(idea) switch (name){ case "一只肥": System.out.println("一只肥"); break; case "二只肥": System.out.println("二只肥"); break; case "三只肥": System.out.println("三只肥"); break; default : System.out.println("蛤"); } } }
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package com.zishi.struct; public class SwitchDemo02 { public SwitchDemo02() { } public static void main(String[] args) { String name = "一只肥"; byte var3 = -1; switch(name.hashCode()) { case 19888123: if (name.equals("一只肥")) { var3 = 0; } break; case 19896772: if (name.equals("三只肥")) { var3 = 2; } break; case 20022663: 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("蛤"); } } }
字符串本质还是通过hashcode哈希值来获取