昨天面试题中有一题是switch语句参数不能为以下哪种?
A.byte B.char C.String D.int
java中switch-case语句的一般格式如下:
- switch(参数) {
- case 常量表达式1: break;
- case 常量表达式2: break;
- ...
- default: break;
- }
note:
switch接受的参数类型有10种,分别是基本类型的byte,short,int,char,以及引用类型的String(只有JavaSE 7 和以后的版本 可以接受String类型参数),enum和byte,short,int,char的封装类Byte,Short,Integer,Character
case 后紧跟常量表达式,不能是变量。
default语句可有可无,如果没有case语句匹配,default语句会被执行。
case语句和default语句后的代码可不加花括号。
如果某个case语句匹配,那么case后面的语句块会被执行,并且如果后面没有break关键字,会继续执行后面的case语句代码和default,直到遇见break或者右花括号。
具体例子如下:
- package sampleTest;
-
- public class TestSwitch {
- public static void main(String[] args) {
- int count = 0;
- switch (count) {
- case 0:
- System.out.println("case0");
- case 1:
- System.out.println("case1");
- case 2:
- System.out.println("case2");
- break;
- case 3:
- System.out.println("case3");
- default:
- System.out.println("default!");
- }
-
- System.out.println("-------------------");
-
- String msg = "dragon";
- switch (msg) {
- case "rabbit":
- System.out.println("rabbit ");
- case "dragon":
- System.out.println("happy new year");
- default:
- System.out.println("what ?");
- case "monkey":
- System.out.println("monkey");
- break;
- case "tiger":
- System.out.println("tiger!!");
- }
- }
- }
输出如下:
case0
case1
case2
-------------------
happy new year
what ?
monkey
上面例子说明了两个问题,第一个是不加break的后果,第二个是default的位置对执行的影响。