JDK 14 – JEP 361 Switch Expressions out from preview

In my previous post, I wrote about switch expressions and related enhancements released as a preview feature for JDK 12. Subsequently, in JDK 13 there were some changes proposed, like using yield keyword to return value from the switch block and released in preview.

In the upcoming JDK 14 release, which will go GA in March next year, these changes in switch will be out from preview and made final and permanent. In this post, we will look at what has changed over the two releases and re-run the examples I shared in my previous post on JDK 14.

Switch as an expression

In the below code snippet we will see how to use switch as an expression i.e evaluate some conditions and return a value:

public static boolean isHealthy(String food){
	return switch (food){
		case "Fruit"  -> true;
		case "Vegetable" -> true;
		case "Pizza" -> false;
		case "Burger" -> false;
		case "Pulses" -> true;
		case "Soft Drink" -> false;
		default -> false;
	};
}

We can use string literals as case expressions while this was not possible prior to JDK 12. The above can be written using Enums in which case we wouldn’t need the default block:

public static Boolean isHealthEnumVersion(Food food){
	return switch (food){
		case Fruit -> true;
		case Vegetable -> true;
		case Pizza -> false;
		case Burger -> false;
		case Pulses -> true;
		case Soft_Drink -> false;
	};
}

The Food enum is defined as:

enum Food {
    Fruit, Vegetable, Pizza, Burger, Pulses, Soft_Drink
}

Switch expression with a block of code for a case evaluation

In the previous example, we saw that the case handled only a single line expression. What if we wanted to execute multiple statements and then return a result? This can be achieved using theyield keyword.

In JDK 12, the break keyword was overloaded to return the value as well. But this wasn’t appreciated by all and hence a new keyword yield was added for returning the value.

The below code snippet executes a block of code and returns a value:

public static PreparedFood prepareFood(Food food){
  return switch (food){
    case Pizza -> {
      System.out.println("doing pizza related operations");
      yield new PreparedFood(food);
    }
    case Burger -> {
      System.out.println("Doing burger related operations ");
      yield new PreparedFood(food);
    }
    default -> {
      System.out.printf("Doing %s related operations\n", food.toString());
      yield new PreparedFood(food);
    }
  };

}

The yield can also be used in the old switch syntax as shown below:

public static PreparedFood prepareFoodOldSyntax(Food food){
  return switch (food){
    case Pizza:
      System.out.println("doing pizza related operations");
      yield new PreparedFood(food);
    case Burger:
      System.out.println("Doing burger related operations ");
      yield new PreparedFood(food);
    default:
      System.out.printf("Doing %s related operations\n", food.toString());
      yield new PreparedFood(food);
  };

}

The source code for this can be found in the repository here.

1 thought on “JDK 14 – JEP 361 Switch Expressions out from preview”

Leave a Reply

Discover more from Experiences Unlimited

Subscribe now to keep reading and get access to the full archive.

Continue reading