-
witch支持部分基本数据类型(primitive data types),如:byte、short、int、long、char;不支持boolean、float、double。
如图的例子:
-
支持Enum类型、String、和部分基本类型的包装类(如:Character、Byte、Short、Integer);
如图的例子:
-
break关键字可以结束switch语句,如果没有break,则从符合条件的case开始一直往下执行到switch结束或者遇到break。如图例子,结果为4:
-
可以有多个case的语句。如图:
witch支持部分基本数据类型(primitive data types),如:byte、short、int、long、char;不支持boolean、float、double。
如图的例子:
支持Enum类型、String、和部分基本类型的包装类(如:Character、Byte、Short、Integer);
如图的例子:
break关键字可以结束switch语句,如果没有break,则从符合条件的case开始一直往下执行到switch结束或者遇到break。如图例子,结果为4:
可以有多个case的语句。如图:
5.
switch支持的类型有?
- Java 7 中加入了对String类型的支持。所以支持的有:char、byte、short、int 和 Character、Byte、Short、Integer 和 String
int type=4;
switch (type) {
default:
System.out.println(4);
case 1:
System.out.println(1);
case 2:
System.out.println(2);
case 3:
System.out.println(3);
}
}
输出结果 4 1 2 3
没有break就一直输出。