In Java, switch statements have traditionally been used to evaluate a value and execute different code blocks based on the value’s match with specific cases. However, starting from Java 14, switch expressions were introduced as a preview feature, allowing for a more concise and expressive syntax. Along with switch expressions, the new yield
keyword was introduced to return a value from a switch case.
Switch Expressions:
Switch expressions in Java provide a more streamlined syntax compared to traditional switch statements. They are expressions, meaning they can produce a value, and they can be used in assignments, return statements, and method parameters.
The syntax of a switch expression is as follows:
result = switch (expression) {
case value1 -> expression1;
case value2 -> expression2;
case value3 -> expression3;
...
default -> expressionDefault;
};
Key points to note about switch expressions:
- The
expression
is evaluated, and the control flow matches the first case that matches the expression’s value. - Each case is followed by
->
and an expression or a block of code. - The
default
case is optional and executed when none of the previous cases match. - The
result
variable stores the value produced by the executed case.
Let’s look at an example to understand switch expressions better:
int day = 3;
String dayName = switch (day) {
case 1 -> "Monday";
case 2 -> "Tuesday";
case 3 -> "Wednesday";
case 4 -> "Thursday";
case 5 -> "Friday";
case 6, 7 -> {
System.out.println("It's the weekend!");
yield "Weekend";
}
default -> throw new IllegalArgumentException("Invalid day: " + day);
};
System.out.println("Today is " + dayName);
In this example, based on the value of the day
variable, the switch expression matches the corresponding case and assigns the result to the dayName
variable. If the value is 6 or 7 (representing the weekend), a block of code is executed, printing a message and using the yield
keyword to return the value “Weekend”. Finally, the value of dayName
is printed.
The Yield Keyword:
The yield
keyword is used to return a value from a switch expression. It can only be used within a block of code and is associated with a particular case. The yield
keyword allows you to both specify a value and terminate the switch expression.
In the previous example, the yield
keyword is used to return the value “Weekend” when the case matches 6 or 7. Without the yield
keyword, the block of code would execute, but the switch expression would continue evaluating other cases.
Conclusion:
Switch expressions, along with the yield
keyword, bring enhanced functionality and readability to switch statements in Java. They allow developers to write more concise code and use switch statements as expressions that produce values. By embracing switch expressions and the yield
keyword, developers can take advantage of a more expressive and streamlined syntax for handling multi-way branching in their Java programs.
Discover more from GhostProgrammer - Jeff Miller
Subscribe to get the latest posts sent to your email.