1.注意switch只能比较数值或字符或者类对象;
2.每个case 必须加 break;f否则会一直执行到 break为止;
3.如果没有找到匹配的case,会执行default语句;
4.实例:
public void switchCase(){
this.net(2);
}
private void net(int network){
switch (network) {
case 1: {
System.out.println("BAIDU");
break;
}
case 2:
case 3: {
System.out.println("BAIDUpeview");
break;
}
case 4: {
break;
}
case 5:
case 6: {
break;
}
case 7:{
break;
}
case 8:
case 9:{
break;
}
default: {
System.out.println("network [" + network +" is not match ....");
break;
}
}
}
4.switch和if else的性能比较;(参考:http://www.l99.com/151931/blog/view/77303)