Java 13 — New enhanced switch

ard-Site
3 min readApr 17, 2020

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.

Requirements

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.

char letter = 'b';switch (letter) {
case 'a' -> System.out.println("This is the letter a");
case 'b' -> System.out.println("This is the letter b");
case 'c' -> System.out.println("This is the letter c");
case 'd' -> System.out.println("This is the letter d");
default -> System.out.println("This is a letter other than a, b, c or d");
}

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:

switch (letter) {
case 'a' : System.out.println("This is the letter a");
case 'b' -> System.out.println("This is the letter b");
case 'c' : System.out.println("This is the letter c");
case 'd' -> System.out.println("This is the letter d");
default -> System.out.println("This is a letter other than a, b, c or d");
}

You will get a compiler error if you do this.

Yield

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

char letter = 'c';
char selectedLetter = switch (letter) {
case 'a' -> {
System.out.println("This is 'a'.");
yield 'a';
}
case 'b' -> {
System.out.println("This is 'b'");
yield 'b';
}
case 'c' -> {
System.out.println("This is 'c'");
yield 'c';
}
case 'd' -> {
System.out.println("This is 'd'");
yield 'd';
}
default -> {
System.out.println("This is a letter other than a, b, c or d");
yield Character.toUpperCase(letter);
}
};

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”:

char letter = 'c';
var selectedValue = switch (letter) {
case 'a' -> 'a';
case 'b' -> 2;
case 'c' -> 3.33;
case 'd' -> "This is 'd'";
default -> "This is a letter other than a, b, c or d";
};

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
“ConstantDesc”.

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

Java 13:
https://openjdk.java.net/projects/jdk/13/

Enhanced Switch:
https://openjdk.java.net/jeps/354

var:
https://openjdk.java.net/projects/amber/LVTIFAQ.html

OpenJDK 13 Download:
https://jdk.java.net/13/

JDK 13 Download:
https://www.oracle.com/java/technologies/javase-jdk13-downloads.html

--

--