Java Switch Expressions: Returning Values with Modern Switch Statements
Explore Java Switch Expressions, introduced in newer versions of Java, which allow you to return a value from a switch statement. Learn how to use the case L ->
label and the yield
statement to return values, improving the flexibility of switch statements.
Java - Switch Expressions
Switch expressions in Java allow us to return a value, making them usable within statements like other expressions. The case L ->
label is used to return a value, or we can use the yield
statement to return a value from the switch expression.
Introduction to Switch Expressions
Switch expressions were introduced as a preview feature in Java 12 and became standard in Java 14. Java 13 added the yield
construct, which allows a switch expression to return a value.
Each case
block in a switch expression can return a value using the yield
statement. If using an enum, the default
case can be skipped. Otherwise, a default
case is required.
Switch Expression Using "case L ->" Labels
In Java, you can return a value from a switch expression using the case L ->
notation.
Syntax
case label1, label2, ..., labeln -> expression;|throw-statement;|block;
Where label1
to labeln
represent values to compare. If a label matches, the right-hand block after the arrow executes, and the result is returned.
Switch Expression Example: Using "case L ->" Labels
This example compares switch expressions with switch statements. The getDayType()
method uses a switch expression, while getDayTypeOldStyle()
uses a traditional switch statement.
Code
package com.tutorialsarena;
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("Invalid"));
System.out.println("New Switch");
System.out.println(getDayType("Monday"));
System.out.println(getDayType("Saturday"));
System.out.println(getDayType("Invalid"));
}
public static String getDayType(String day) {
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;
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;
}
}
Output
Old Switch
Weekday
Weekend
Invalid day.
New Switch
Weekday
Weekend
Invalid day.
Switch Expression Using "case L:" Statements and Yield
With the yield
statement, a value can be returned from a switch expression or switch statement after performing an operation.
Syntax
case label1, label2, ..., labeln -> expression; yield value;
case label1:
case labeln: expression; yield value;
Switch Expression Example: Using Yield Statement
In this example, the getDayType()
method uses a switch expression with yield, while getDayTypeStyle2()
uses the traditional case L:
statement with yield.
Code
package com.tutorialsarena;
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("New Way");
System.out.println(getDayType("Monday"));
System.out.println(getDayType("Saturday"));
System.out.println(getDayType("Invalid"));
}
public static String getDayType(String day) {
return switch (day) {
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);
};
}
}
Output
Old Way
Weekday
Weekend
New Way
In Weekdays
Weekday
In Weekends
Weekend
Exception in thread "main" java.lang.IllegalStateException: Invalid day:
at com.tutorialsarena.SwitchTester.getDayType(SwitchTester.java:26)
at com.tutorialsarena.SwitchTester.main(SwitchTester.java:13)