100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > Switch参数的类型及Switch--case的穿透性

Switch参数的类型及Switch--case的穿透性

时间:2019-02-02 13:44:08

相关推荐

Switch参数的类型及Switch--case的穿透性

Switch参数的类型及Switch–case的穿透性

Switch参数的类型

switch目前支持的参数类型是 byte short char int enum String 其中 enum是java1.5引入,String是java1.7引入

Switch–case的穿透性

switch case语句具有穿透性,如果上面的语句执行后,不使用break或者continue,case语句将会继续往下执行,直到遇见终止句或者将case语句执行完毕,不会考虑是否满足下面case的要求代码演示没有结束语句

public class Demo1 {public static void main(String[] args) {int i = 1;//如果不书写结束语句switch (i) {case 1 :System.out.println(1);case 2:System.out.println(2);case 3:System.out.println(3);case 4:System.out.println(4);case 5:System.out.println(5);}}}

执行结果

-中间有结束语句

public class Demo1 {public static void main(String[] args) {int i = 1;switch (i) {case 1 :System.out.println(1);case 2:System.out.println(2);case 3:System.out.println(3);break;case 4:System.out.println(4);case 5:System.out.println(5);}}}

执行结果

正常

public class Demo1 {public static void main(String[] args) {int i = 1;switch (i) {case 1 :System.out.println(1);break;case 2:System.out.println(2);break;case 3:System.out.println(3);break;case 4:System.out.println(4);break;case 5:System.out.println(5);break;}}}

执行结果

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。