switch case :在JDK 7中,又加入了对String类型的支持,从此不用再写If-Else来判断字符串了

时间:2023-03-10 04:36:52
switch   case   :在JDK 7中,又加入了对String类型的支持,从此不用再写If-Else来判断字符串了
switch的case语句可以处理int,short,byte,char类型的值,
因为short,byte,char都会转换成int进行处理,这一点也可以从生成的字节码看出。
  1. char a = 'e';
  2. switch (a) {
  3. case 'c':
  4. System.out.println("In case c");
  5. break;
  6. case 'd':
  7. System.out.println("In case d");
  8. break;
  9. default:
  10. System.out.println("In default");
  11. break;
  12. case 'e':
  13. System.out.println("In case e");
  14. break;
  15. }
  16. 在JDK 5中加入的枚举Enum类型也是可以作为case值的。
  17. 在JDK 7中,又加入了对String类型的支持,从此不用再写If-Else来判断字符串了。