与if-then
和if-then-else
语句不同,switch
语句可以具有许多可能的执行路径。switch
适用于byte
,short
,char
和int
基本数据类型。它还适用于枚举类型( 枚举类型中讨论), String
类,以及一些些基本类型的包装类: Character
, Byte
, Short
,和 Integer
( 数字和字符串中讨论)。
以下代码示例 SwitchDemo
声明了一个int
类型,名为month
的字段,其值表示月份。以下代码使用switch
语句基于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
块中的语句可以用一个或多个case
或default
标签来标记。switch
语句计算其表达式,然后执行匹配case
标签后的所有语句。
您还可以使用以下if-then-else
语句显示月份名称:
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
从技术上讲,由于控制流不在该switch
语句之内,因此不需要最终的break
。建议使用break
,这样可以更轻松地修改代码,并且不易出错。default
语句处理所有未由其中一个case
语句明确处理的值。
以下代码示例 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语句中使用字符串
在Java SE 7和更高版本中,可以在switch
语句的表达式中使用String
对象。以下代码示例, StringSwitchDemo
基于String
类型,名为month
的值显示月份数:
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
表达式和与每个case
标签关联的String
进行比较,就好像使用String.equals
方法一样。为了使StringSwitchDemo
示例无论大小写都可以接受任何月份,将month
转换为小写(使用 toLowerCase
方法),并且与case
标签关联的所有字符串都小写。
注意:此示例检查了switch
语句中的表达式是否为null
。确保任何switch
语句中的表达式都不为空,以防止NullPointerException
异常。