In Java 13, the Test.java:Switch Expressions extends the previousJava 12 Switch Expressions by adding a new yield
keyword to return a value from switch expression.
P.S :Switch expressions are a preview feature and are disabled by default.
1. No more value breaks!
1.1 The below Java 12 value breaks
syntax is no longer compiled in Java 13, uses yield
instead.
1 | // value breaks are superseded by Java 13 'yield' statements. |
1.2 In Java 13, uses yield
to return a value from switch
1 | private static int Test(String demo) { |
1.3 Return values vialabel rules or arrows like lambod expression from a switch
expression (Java 12) is still supported in Java 13.
1 | private static int Test(String demo) { |
2. Java 13 Switch Expressions
A full switch
expression example in Java 13.
1 | package com.chanzin.java13; |
3. Enable preview features
3.1 A normal javac
compile will prompts the following errors:
3.2 We need to enable the preview feature manually:
1 | javac --enable-preview --release 13 Test.java |
)