English English

Warning: count(): Parameter must be an array or an object that implements Countable in /var/www/ard-site/templates/ardsite/library/Designer/Content/SingleArticle.php on line 198

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.

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

We use cookies on our website. Some of them are essential for the operation of the site, while others help us to improve this site and the user experience (tracking cookies). You can decide for yourself whether you want to allow cookies or not. Please note that if you reject them, you may not be able to use all the functionalities of the site.

Ok