原文出自 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); } } }}
代碼輸出是:
AugustSeptemberOctoberNovemberDecember
從技術層面講,最後的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。