Java Switch Expressions

By Arvind Rai, July 15, 2021
Java switch expressions are same like other expressions and evaluate to a single value. They contain labels like case L -> and eliminate the need of break statements to prevent fall through. To yield a value in traditional switch statement, yield keyword has been introduced.
Now switch can be used as following.
1. The switch can be used either statement or an expression but cannot be mixed together.
2. The switch expression has no fall through with following case label form.
case label_1, label_2, ..., label_n -> expression;|throw-statement;|block 
With single label:
case VARANASI -> System.out.println("Varanasi"); 
With multiple label:
case MADURAI, DELHI -> {
  int len = "Madurai".length() + "Delhi".length();
  System.out.println(len);
} 
Returning value:
case 2 -> 2 * 10; 
3. The switch statement case label is used as case L : with fall through.
case BANGALORE: 
  System.out.println("Bangalore City");
  break; 
4. The switch statement can yield a value using yield keyword.
case BANGALORE: 
  System.out.println("Bangalore City");
  yield "Bangalore"; 
5. The switch expression was introduced in Java 12 as preview feature and previewed again in Java 13. The switch expression has been adopted as permanent language feature in Java 14 (JEP 361).

Using Single Label

Here we will create a switch expression example with single label.
package com.concretepage;
enum City {
  VARANASI, BANGALORE, MADURAI, DELHI;
}
public class MyApp {
  public static void main(String[] args) {
	City city = City.BANGALORE;
	switch (city) {
	  case VARANASI -> System.out.println("Varanasi");
	  case BANGALORE -> System.out.println("Bangalore");
	  case MADURAI -> System.out.println("Madurai");
	  case DELHI -> System.out.println("Delhi");
	  default -> throw new IllegalStateException("Invalid city: " + city);
	}
  }
} 
Output is Bangalore.
Find one more example.
City city = City.BANGALORE;
switch (city) {
  case VARANASI -> {
	int len =  "Varanasi".length();
	System.out.println(len);
  }
  case BANGALORE -> {
	int len = "Bangalore".length();
	System.out.println(len);
  }
  case MADURAI -> {
	int len = "Madurai".length();
	System.out.println(len);
  }
  case DELHI -> {
	int len = "Delhi".length();
	System.out.println(len);
  }
  default -> {
	throw new IllegalStateException("Invalid city: " + city);
  }
} 
Output is 9.
We can see that we didn't need break statements to prevent fall through.

Using Multiple Labels

Find the switch expression example with multiple labels.
City city = City.BANGALORE;
switch (city) {
  case VARANASI, BANGALORE -> System.out.println("Varanasi".length() + "Bangalore".length());
  case MADURAI, DELHI -> {
	int len = "Madurai".length() + "Delhi".length();
	System.out.println(len);
  }
  default -> throw new IllegalStateException("Invalid city: " + city);
} 
Output is 17.

Returning Value using Expression

We can return a value using switch expression as following.
City city = City.BANGALORE;
String data = switch (city) {
  case VARANASI, BANGALORE -> "Varanasi and Bangalore";
  case MADURAI, DELHI -> "Madurai and Delhi";
  default -> throw new IllegalStateException("Invalid city: " + city);
};
System.out.println(data); 
Find one more example.
int num = 2;
int output  = switch (num) {
  case 1 -> 1 * 10;
  case 2 -> 2 * 10;
  case 3 -> 3 * 10;
  case 4 -> 4 * 10;
  default -> throw new IllegalStateException("Invalid number: " + num);
};
System.out.println(output); 
Output is 20.

Using yield

In traditional switch statement, we can return a value using yield keyword.
City city = City.BANGALORE;
String data = switch (city) {
  case VARANASI:
  case MADURAI:
	System.out.println("Madurai City");
	yield "Madurai";
  case BANGALORE: 
	System.out.println("Bangalore City");
	yield "Bangalore";		
  case DELHI:
	System.out.println("Delhi City");
	yield "Delhi";
  default:
  throw new IllegalStateException("Invalid city: " + city);
};
System.out.println(data); 

References

Switch Expressions
JEP 361: Switch Expressions
POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us