Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
switch expressions
communitycreator

What are switch expressions in Java?

Programming Bytes

A switch expression is like a normal expression, except that it will be evaluated to a value. This is an improvement of the switch statement. In a switch expression:

  • Each case supports multiple labelsvalues.
  • It can return value by arrow label and yield statementIntroduced in Java SE 13.

Traditional switch statement

class HelloWorld {
    public static void main( String args[] ) {
        String browserSupport;
        String browser = "Safari";
        switch (browser) {
          case "IE":
            browserSupport = "Un-Supported Browser";
            break;
          case "Chrome":
          case "Safari":
          case "Opera":
            browserSupport = "Supported Browser";
            break;
          default:
            browserSupport =  "Unknown Browser";
        }
        System.out.println(browserSupport);
    }
}

In the above code, we have used a switch statement to check the browser support.

Using switch expressions

Let’s write the above code with switch expressions.

1. Using yield to return value

public class HelloWorld {
    public static void main(String[] args) {
      String browser = "Safari";
      String browserSupport = switch (browser) {
        case "IE":
          yield "Un-Supported Browser";
          
        case "Chrome", "Safari", "Opera":
          yield "Supported Browser";
          
        default:
          yield "Unknown Browser";
      }
      
      System.out.println(browserSupport);
  }
}

In the above example, we have replaced the multiple case label in single case label.

case "Chrome":
case "Safari":
case "Opera":

is replaced by

case "Chrome", "Safari", "Opera":

We have also used yield to return a value from the switch expression.


2. Using arrow label to return value

public class HelloWorld {
    public static void main(String[] args) {
      String browser = "Safari";
      String browserSupport = switch (browser) {
        case "IE" -> "Un-Supported Browser";
        case "Chrome", "Safari", "Opera" -> "Supported Browser"; 
        default -> "Unknown Browser";
      }
      
      System.out.println(browserSupport);
  }
}

In the above code, we have replaced the yield statement with an arrow label (->). The colon (:) at the end of the case label is also removed. By adding a switch expression we are able to make the code shorter and more readable.


When using the switch expression, it must either complete normally, with a value, or complete abruptly by throwing an exception.

String browser = "Opera";
String browserSupport = switch (browser) {
        case "IE" -> "Un-Supported Browser";
        default -> System.out.println("Compilation error");
}

You can install Java 14 on your machine to test the switch expression. Download Java 14 here.

When we create a switch expression that doesn’t yield a value (like in the code above), it will throw an error as //Group doesn't contain a yield statement.

You can read more about switch expressions on the official page: JEP 361- Java 14 .

RELATED TAGS

java
switch expressions
communitycreator
RELATED COURSES

View all Courses

Keep Exploring