原文出自 http://www.cnblogs.com/ggjucheng/archive/2012/12/16/2820839.html
英文出自 http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
不像if-then和
if-then-else语句,switch语句可以有多个可能的执行路径。switch工作于于
byte
,short
, char
, int原生类型。它也作用于枚举类型,String类和几个特殊的原生类型包装类:
Character
, Byte
, Short
, and Integer。
下面的代码例子,SwitchDemo,声明一个名称为month的int类型,它的值表示月份。代码使用switch语句,根据month的值,展示month的名字。
public class SwitchDemo { public static void main(String[] args) { int month = 8; String monthString; switch (month) { case 1: monthString = "January"; break; case 2: monthString = "February"; break; case 3: monthString = "March"; break; case 4: monthString = "April"; break; case 5: monthString = "May"; break; case 6: monthString = "June"; break; case 7: monthString = "July"; break; case 8: monthString = "August"; break; case 9: monthString = "September"; break; case 10: monthString = "October"; break; case 11: monthString = "November"; break; case 12: monthString = "December"; break; default: monthString = "Invalid month"; break; } System.out.println(monthString); } }
这个情况下,输出August到标准输出。
switch语句的正文,如所知是一个switch块。switch块的语句,可以标记一个或多个default标签,switch语句计算它的表达式,然后执行对应的case标签跟随的所有代码。
使用if-then-else语句,显示month对应的名字:
int month = 8; if (month == 1) { System.out.println("January"); } else if (month == 2) { System.out.println("February"); } ... // and so on
决策何时使用if-then-else语句和switch语句,是根据表达式语句被测试的可读性。if-then-else语句可以测试表达式的值的范围和条件表达式,而switch语句对表达式的测试,只能基于数字,枚举类型,String对象。
另外一个兴趣点是break语句。每个break语句都可以结束switch语句封闭块。控制流从switch块的第一个语句开始持续,break语句是必须的,没有了它,switch块的语句会告吹:符合的case标签后所有的代码,会按照顺序执行,不用管后面的case标签的表达式,一直到一个break语句。程序SwitchDemoFallThrough
显示switch块的语句失败。程序根据整数month,显示一年的月份:
public class SwitchDemoFallThrough { public static void main(String args[]) { java.util.ArrayList<String> futureMonths = new java.util.ArrayList<String>(); int month = 8; switch (month) { case 1: futureMonths.add("January"); case 2: futureMonths.add("February"); case 3: futureMonths.add("March"); case 4: futureMonths.add("April"); case 5: futureMonths.add("May"); case 6: futureMonths.add("June"); case 7: futureMonths.add("July"); case 8: futureMonths.add("August"); case 9: futureMonths.add("September"); case 10: futureMonths.add("October"); case 11: futureMonths.add("November"); case 12: futureMonths.add("December"); break; default: break; } if (futureMonths.isEmpty()) { System.out.println("Invalid month number"); } else { for (String monthName : futureMonths) { System.out.println(monthName); } } } }
代码输出是:
August September October November December
从技术层面讲,最后的break语句非必须,因为控制流已经离开了switch语句。使用break是推荐的,因为修改代码比较容易,减少易出现的错误。如果case片段的每一个值都无法处理,那么default片段会处理所有的值。
下面的代码例子,SwitchDemo2,显示一个语句可以有多个case标签。代码例子计算特定月份的天数:
class SwitchDemo2 { public static void main(String[] args) { int month = 2; int year = 2000; int numDays = 0; switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: numDays = 31; break; case 4: case 6: case 9: case 11: numDays = 30; break; case 2: if (((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0)) numDays = 29; else numDays = 28; break; default: System.out.println("Invalid month."); break; } System.out.println("Number of Days = " + numDays); } }
代码输出是:
Number of Days = 29
switch语句使用Strings
java7及后续版本,可以在switch语句的表达式中使用String对象。接下来的代码例子,StringSwitchDemo,根据月份的名字,显示月份的整数值:
public class StringSwitchDemo { public static int getMonthNumber(String month) { int monthNumber = 0; if (month == null) { return monthNumber; } switch (month.toLowerCase()) { case "january": monthNumber = 1; break; case "february": monthNumber = 2; break; case "march": monthNumber = 3; break; case "april": monthNumber = 4; break; case "may": monthNumber = 5; break; case "june": monthNumber = 6; break; case "july": monthNumber = 7; break; case "august": monthNumber = 8; break; case "september": monthNumber = 9; break; case "october": monthNumber = 10; break; case "november": monthNumber = 11; break; case "december": monthNumber = 12; break; default: monthNumber = 0; break; } return monthNumber; } public static void main(String[] args) { String month = "August"; int returnedMonthNumber = StringSwitchDemo.getMonthNumber(month); if (returnedMonthNumber == 0) { System.out.println("Invalid month"); } else { System.out.println(returnedMonthNumber); } } }
代码输出是8.
switch表达式的String,对比每个case标签关联的表达式,String.equals
会被使用。为了让StringSwitchDemo例子接受任何月份的名字,不考虑大小写,month被转换为小写(使用
toLowerCase方法
),而case标签关联的字符串都是用小写。
注意:例子检查switch语句中的表达式是否null。确定switch语句的任何表达式不是null,避免抛出一个NullPointerException。