switch的使用

ji本没用过这个函数,今天用到了它,发现了一些使用要注意的地方:

switch的参数支持int和枚举,单jdk1.7后,开始支持String类型。我特意在jdk1.8上试了试,

public class SwitchTest {
public static void main (String [] args){
String a="1";
switch (a){
case "1":System.out.println("ok"); break;
default:System.out.println("no");
}

}
}

结果如下:

Connected to the target VM, address: '127.0.0.1:60125', transport: 'socket'
ok
Disconnected from the target VM, address: '127.0.0.1:60125', transport: 'socket'

Process finished with exit code 0

说明,没问题;

在jdk1.7以下的版本人会提示参数不可使用String类型。

 

没踩过的坑,有时候踩一下,才踏实。



posted @ 2016-12-20 10:46  it馅儿包子  阅读(259)  评论(0编辑  收藏  举报