Java 简明教程
Java - Switch Expressions
使用 switch expressions,我们能够返回一个值并且可以在语句中将它们用作其他表达式一样。我们能够使用 case L → label 返回一个值或者使用 yield,我们能够从 switch 表达式中返回一个值。
Using switch expressions, we can return a value and we can use them within statements like other expressions. We can use case L → label to return a value or using yield, we can return a value from a switch expression.
Java 12 引入了 expressions to Switch statements,并作为预览功能发行了它们。Java 13 添加了新的 yield 构造以从 switch 语句返回一个值。凭借 Java 14,switch expression 现已成为一个标准功能。
Java 12 introduces expressions to Switch statements and releases them as a preview feature. Java 13 added a new yield construct to return a value from a switch statement. With Java 14, switch expression now is a standard feature.
-
Each case block can return a value using yield statement.
-
In case of enum, default case can be skipped. In other cases, default case is required.
Switch Expression Using "case L →" Labels
Java 提供了一个利用 case L - > 符号从 switch 表达式中返回一个值的新方法。
Java provides a new way to return a value from a switch expression using case L - > notation.
Syntax
case label1, label2, ..., labeln -> expression;|throw-statement;|block
其中 label1 至 labeln 表示要比较的不同值,并且我们能够利用表达式(一个语句)来返回一个值或抛出一个表达式。
Where label1 to labeln are representing the various values to compare and we can use expression, a statement to return a value or throw an expression.
一旦 Java 运行时将左块中的任何标牌与箭头匹配,则它将移动到箭头的右块并执行表达式(语句),然后返回结果。
Once Java runtime matches any label in the left block to the arrow, it moves to the right block of arrow and executes the expression(statement) and returns the result.
Switch Expression Example: Using case L → Labels
在此示例中,我们已经比较了 switch 表达式与 switch 语句。getDayType() 方法接收一个字符串输入并且通过评估 switch 表达式并返回该值来返回相应的条目。而 getDayTypeOldStyle() 方法则使用 switch 语句来比较每个 case,使用 break 语句来结束 switch case,并且为了获取一个值,我们必须在一个变量中设置该值。
In this example, we’ve compared switch expressions vs switch statement. getDayType() method takes a string input and returns the corresponding entry by evaluation switch expression and returning the value. Whereas getDayTypeOldStyle() method uses switch statement to compare each cases, uses break statement to end switch cases and to get a value, we’ve to set value in a variable.
package com.tutorialspoint;
public class SwitchTester {
public static void main(String[] args) {
System.out.println("Old Switch");
System.out.println(getDayTypeOldStyle("Monday"));
System.out.println(getDayTypeOldStyle("Saturday"));
System.out.println(getDayTypeOldStyle(""));
System.out.println("New Switch");
System.out.println(getDayType("Monday"));
System.out.println(getDayType("Saturday"));
System.out.println(getDayType(""));
}
public static String getDayType(String day) {
// evaluate switch expression and get a value
return switch (day) {
case "Monday", "Tuesday", "Wednesday","Thursday", "Friday" -> "Weekday";
case "Saturday", "Sunday" -> "Weekend";
default -> "Invalid day.";
};
}
public static String getDayTypeOldStyle(String day) {
String result = null;
// evaluate relevant cases and get a value into result variable
switch (day) {
case "Monday":
case "Tuesday":
case "Wednesday":
case "Thursday":
case "Friday":
result = "Weekday";
break;
case "Saturday":
case "Sunday":
result = "Weekend";
break;
default:
result = "Invalid day.";
}
return result;
}
}
让我们编译并运行上述程序,这将生成以下结果 −
Let us compile and run the above program, this will produce the following result −
Old Switch
Weekday
Weekend
Invalid day.
New Switch
Weekday
Weekend
Invalid day.
Switch Expression Using "case L:" Statements and the yield Statement
在 Java 中,我们能够利用 switch 表达式和 switch 语句以及 yield 语句来获取一个值。yield 语句返回该值并完成 switch 执行。
In java, we can get a value from switch expressio and switch statement using yield statement. yield statement returns the value and finishes the switch execution.
Syntax
case label1, label2, ..., labeln -> expression; yield value;
case label1:
case labeln: expression;
yield value;
Switch Expression Example: Using "case L:" Statements and the yield Statement
其中 label1 至 labeln 表示要比较的不同值,并且我们能够利用表达式来执行一个操作并利用 yield 语句来返回该值。
Where label1 to labeln are representing the various values to compare and we can use expression to perform an operation and return value using yield statement.
在此示例中,我们已经利用 yield 语句来比较 swtich 表达式。getDayType() 方法正在利用一个带有 yield 语句的 switch 表达式来获取该结果,而 getDayTypeStyle1() 方法正在利用带有带有 yield 语句的冒号 (:) 符号的 switch case 来获取该期望结果。
In this example, we’ve compared switch expressions using yield statements. getDayType() method is using a switch expression with yield statement to get the result whereas getDayTypeStyle1() method is using switch cases with colon(:) notation with yield statement to the get the desired result.
package com.tutorialspoint;
public class SwitchTester {
public static void main(String[] args) {
System.out.println("Old Way");
System.out.println(getDayTypeStyle2("Monday"));
System.out.println(getDayTypeStyle2("Saturday"));
// System.out.println(getDayTypeStyle2(""));
System.out.println("New Way");
System.out.println(getDayType("Monday"));
System.out.println(getDayType("Saturday"));
System.out.println(getDayType(""));
}
public static String getDayType(String day) {
return switch (day) {
// we can use block statements to return a value using yield after
// executing other statements
case "Monday", "Tuesday", "Wednesday","Thursday", "Friday" -> {
System.out.println("In Weekdays");
yield "Weekday";
}
case "Saturday", "Sunday" -> {
System.out.println("In Weekends");
yield "Weekend";
}
default -> throw new IllegalStateException("Invalid day: " + day);
};
}
public static String getDayTypeStyle2(String day) {
return switch (day) {
case "Monday":
case "Tuesday":
case "Wednesday":
case "Thursday":
case "Friday":
yield "Weekday";
case "Saturday":
case "Sunday":
yield "Weekend";
default:
throw new IllegalStateException("Invalid day: " + day);
};
}
}
让我们编译并运行上述程序,这将生成以下结果 −
Let us compile and run the above program, this will produce the following result −
Old Way
Weekday
Weekend
New Ways
In Weekdays
Weekday
In Weekends
Weekend
Exception in thread "main" java.lang.IllegalStateException: Invalid day:
at com.tutorialspoint.SwitchTester.getDayType(SwitchTester.java:26)
at com.tutorialspoint.SwitchTester.main(SwitchTester.java:13)