Switch expressions (enhancements) in Java 14

Java 14 introduces a new enhancement to switch statements called switch expressions. Switch expressions provide a more concise and expressive way to write switch statements, making the code easier to read and maintain. In this blog post, we will explore how switch expressions work and how they can be used in your Java code.

What are switch expressions?

Switch expressions extend the functionality of traditional switch statements by allowing them to be used as expressions rather than just control flow constructs. This means that switch expressions can return a value, making them more versatile and powerful.

Syntax of switch expressions

The new syntax for switch expressions is as follows:

result = switch (expression) {
    case value1 -> expression1;
    case value2 -> expression2;
    case value3 -> expression3;
    ...
    default -> expression;
};

Here, expression is the value to be evaluated, and each case specifies the possible values and the corresponding expressions to be executed. The default case is optional and is executed if none of the other cases match.

One notable change from traditional switch statements is the use of -> instead of : to separate the case label from the expression. This aligns the syntax of switch with other language features like lambda expressions and arrow functions.

Benefits of switch expressions

Switch expressions offer several benefits over traditional switch statements:

1. Conciseness

Switch expressions allow you to write more concise code by eliminating the need for break statements. Each case in a switch expression automatically falls through to the next case unless terminated explicitly with break.

2. Compact syntax

The new syntax for switch expressions is more compact and easier to read, especially when there are multiple cases to handle. This improves code maintainability and readability.

3. Use as expressions

Switch expressions can be used as expressions, which means they can return a value. This enables you to assign the result of a switch expression to a variable or use it directly in an expression.

Example usage

Let’s see an example of how switch expressions can be used:

public String getStatus(int statusCode) {
    String status = switch (statusCode) {
        case 200 -> "OK";
        case 404 -> "Not Found";
        case 500 -> "Internal Server Error";
        default -> "Unknown";
    };
    return status;
}

In this example, the getStatus method takes an int parameter statusCode and uses a switch expression to determine the corresponding status message. The result is then assigned to the status variable and returned.

Conclusion

Switch expressions in Java 14 provide a more concise and expressive way to write switch statements. They offer benefits such as conciseness, compact syntax, and use as expressions. By leveraging switch expressions, you can improve the readability and maintainability of your code. Make sure to try out switch expressions in Java 14 and take advantage of their enhanced capabilities.

#java #switchexpressions