Switch


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哈希值來獲取


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM