在Switch语句的使用中,值得注意了解的是下面两点:
1) 如果没有break, 会发生case穿透的现象;
package superlily.control;
public class SwitchDemo {
public static void main(String[] args) {
String input = "Super Lily";
switch (input) {
case "Super Lily":
System.out.println("Super Lily!");
// break; //after comment it out, "Lily is great!" will be printed out
case "Lily is great!":
System.out.println("Lily is great!");
break; //"Nice~" won't be printed out as this block has "break".
default:
System.out.println("Nice~");
}
}
}
这个程序最后输出
Super Lily!
Lily is great!
2) case 后面带字符串的话也是转换成数值来做的,我们可以顺便了解一下反编译是怎么回事,怎么通过IDEA反编译class文件并查看(惊喜~~~~撒花~~~~~~)
从下面这张图里你就能看到字符串在Java里面其实还是数字,java会把它们编译成数字运行我们的程序,是不是很神奇呀~~~~~ :)
IDEA的反编译功能为我们打开了查看源码的一扇窗, 要好好用起来哟,听说经常查看源码战斗力会暴涨哟~