Java 13 — New enhanced switch

Now it is possible to create a switch with other syntax. This feature was introduced as a suggested in Java 12 and it is now available as a preview feature in Java 13.


You have to enable or add the command “ — enable-preview” in your project properties of your IDE.

Besides the usual way to create a switch in Java. Now you can use an arrow and yield for your switch cases. But you can also the return value from a switch case to a variable.

Switch case with arrows

If you use an arrow instead of an double point “:”, then you do not have to add a “break;” in every Switch case to avoid fall-through through your Switch cases.

If the variable “letter” has the value “b”, then case 2 with the output “This is the letter b” is displayed only. You do not need a “break” in this case.

Notice: It is not possible to use a double point and an arrow in a Switch like this:

You will get a compiler error if you do this.


The function “yield” can be used in a Switch statement to return a value in a switch case to a variable.

The output “ This is ‘c’ “ is displayed and the character “c” will be saved to the variable “selectedLetter”.

You have to be aware that yield is a reserved word in Switch statements from now on. But you can still use “yield” as a function name, if you call it by the class where it was defined.

Using different data types as return value in Switch cases

You can also use different data types (such as String, Char, Integers) in your Switch cases of your Switch, when you declare a variable object “var”:

If you do that, then you will get a Java object which is neither of the data types that are used in the Switch cases.
But instead the compiler creates a Java object (“type inference”) that encompasses the data types (used in the Switch cases) and implements the interfaces “Serializable”, “Comparable”, “Constable”, and

If you want to know more about Java 13 and the enhanced Switch, then please check the links below.

Java 13:

Enhanced Switch:


OpenJDK 13 Download:

JDK 13 Download:

Tutorials about Linux, Java and information about IT —

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store