Java教程是为JDK 8编写的。本页面中描述的示例和实践不利用后续版本引入的改进,并可能使用不再可用的技术。
有关Java SE 9及后续版本中更新的语言特性的摘要,请参阅Java语言更改。
有关所有JDK版本的新功能、增强功能以及已删除或弃用选项的信息,请参阅JDK发行说明。
与if-then和if-then-else语句不同,switch语句可以有多个可能的执行路径。 switch与byte、short、char和int原始数据类型一起工作。它还可以与枚举类型(在枚举类型中讨论)、String类和一些包装特定原始类型的特殊类一起使用:Character、Byte、Short和Integer(在数字和字符串中讨论)。
以下代码示例SwitchDemo声明了一个名为month的int,其值表示一个月份。根据month的值,代码使用switch语句显示月份的名称。
public class SwitchDemo {
public static void main(String[] args) {
int month = 8;
String monthString;
switch (month) {
case 1: monthString = "一月";
break;
case 2: monthString = "二月";
break;
case 3: monthString = "三月";
break;
case 4: monthString = "四月";
break;
case 5: monthString = "五月";
break;
case 6: monthString = "六月";
break;
case 7: monthString = "七月";
break;
case 8: monthString = "八月";
break;
case 9: monthString = "九月";
break;
case 10: monthString = "十月";
break;
case 11: monthString = "十一月";
break;
case 12: monthString = "十二月";
break;
default: monthString = "无效月份";
break;
}
System.out.println(monthString);
}
}
在这种情况下,八月被打印到标准输出。
switch语句的主体称为switch块。 switch块中的语句可以带有一个或多个case或default标签。 switch语句评估其表达式,然后执行与匹配的case标签后的所有语句。
你还可以使用if-then-else语句来显示月份的名称:
int month = 8;
if (month == 1) {
System.out.println("一月");
} else if (month == 2) {
System.out.println("二月");
}
... // 其他月份的判断
决定使用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("一月");
case 2: futureMonths.add("二月");
case 3: futureMonths.add("三月");
case 4: futureMonths.add("四月");
case 5: futureMonths.add("五月");
case 6: futureMonths.add("六月");
case 7: futureMonths.add("七月");
case 8: futureMonths.add("八月");
case 9: futureMonths.add("九月");
case 10: futureMonths.add("十月");
case 11: futureMonths.add("十一月");
case 12: futureMonths.add("十二月");
break;
default: break;
}
if (futureMonths.isEmpty()) {
System.out.println("无效的月份编号");
} else {
for (String monthName : futureMonths) {
System.out.println(monthName);
}
}
}
}
这是代码的输出结果:
八月 九月 十月 十一月 十二月
从技术上讲,最后一个break是不必要的,因为流程会跳出switch语句。建议使用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("无效的月份。");
break;
}
System.out.println("天数 = "
+ numDays);
}
}
这是代码的输出:
天数 = 29
switch语句中使用字符串在Java SE 7及更高版本中,你可以在switch语句的表达式中使用String对象。下面的代码示例StringSwitchDemo根据名为month的String的值显示月份的数字:
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 = "八月";
int returnedMonthNumber =
StringSwitchDemo.getMonthNumber(month);
if (returnedMonthNumber == 0) {
System.out.println("无效的月份");
} else {
System.out.println(returnedMonthNumber);
}
}
}
这段代码的输出是8。
在switch表达式中,将字符串与每个case标签关联的表达式进行比较,就好像使用了String.equals方法一样。为了使StringSwitchDemo示例接受任意大小写的月份,将month转换为小写(使用toLowerCase方法),并且所有与case标签关联的字符串都是小写。
注意:此示例检查switch语句中的表达式是否为null。确保任何switch语句中的表达式不为null,以防止抛出NullPointerException。