References:
- "JDK 13 Release Notes", including "What's New in JDK 13" @https://www.oracle.com/technetwork/java/13-relnote-issues-5460548.html.
- OpenJDK's JDK 13 @ https://openjdk.java.net/projects/jdk/13/.
- "81 New Features and APIs in JDK 13" @ https://www.azul.com/jdk-13-81-new-features-and-apis/.
JDK 13 was released on September 17, 2019.
JDK 13 New Language Features
JDK 13 introduces 2 new language features:
- JEP 354: Switch Expressions (Preview)
- JEP 355: Text Blocks (Preview)
Switch Expression (Preview) (tools/javac) (JEP 354)
Reference: JEP 354: Switch Expressions (Preview) @ https://openjdk.java.net/jeps/354.
Notes: JDK 13's JEP 354 supersedes JDK 12's JEP 325.
JDK 12/13 extends the switch construct so that it can be used as either a statement or an expression. This simplifies everyday coding, and prepares the way for the use of pattern matching (JEP 305) in switch.
The original switch statement (follows the C/C++ language) has several irregularities, such as:
- the default control flow behavior between switch labels (i.e., fall through without a breakstatement)
- the default scoping in switchblock (the whole block is treated as one scope)
- switchworks only as a statement, not as an expression.
Arrow Labels (JDK 12 Preview)
The original switch's label has the form of "case L:". JDK 12/13 introduces arrow labels, in the form of "case L ->", which does not fall thru to the next case and break is not needed.
For example:
public class TryJDK13SwitchArrowLabel {
   public static void main(String[] args) {
      String day = "Wednesday";   // switch on String supported since JDK 7
      // Original switch statement with label "case: L" and "break"
      switch (day) {
         case "Monday":
            System.out.println("1");
            break;         // break needed, otherwise fall thru
         case "Tuesday":   // fall thru without break
         case "Wednesday":
         case "Thursday":
            System.out.println("2 or 3 or 4");
            break;
         case "Friday":
            System.out.println("5");
            break;
         default:
            System.out.println("others");
            System.out.println("try again");
      }
      // JDK 13 (Preview) introduces "arrow labels"
      day = "Sunday";
      switch (day) {
         case "Monday" ->
            System.out.println("1");                // use '->' and no break needed
         case "Tuesday", "Wednesday", "Thursday" -> // multiple labels are commas separated
            System.out.println("2 or 3 or 4");
         case "Friday" ->
            System.out.println("5");
         default -> {                               // braces needed for block
            System.out.println("others");
            System.out.println("try again");
         }
      }
   }
}
Notes:
- Multiple labels are separated by commas.
- Body blocks must be enclosed in braces.
To compile and run the program with "preview" features, you need to use the --enable-preview flag to unlock the preview features:
// Compile (with preview feature) > javac TryJDK13SwitchArrowLabel.java TryJDK13SwitchArrowLabel.java:24: error: switch rules are a preview feature and are disabled by default. (use --enable-preview to enable switch rules) > javac --enable-preview --release 13 TryJDK13SwitchArrowLabel.java // Run (with preview feature) > java --enable-preview TryJDK13SwitchArrowLabel
Switch Expressions (JDK 12 Preview) and yield Statement (JDK 13 Preview)
The original switch works only as a statement. JDK 12/13 proposes to use switch as an expression that yields a value. For example,
public class TryJDK13SwitchExpr {
   public static void main(String[] args) {
      String day = "Sunday";   // switch on String supported since JDK 7
      // JDK 13 (Preview) switch expression that evaluates to a value
      int numLetters =     // Assign the switch expression to a variable
         switch (day) {
            case "Monday", "Friday" -> 6;   // single-line expression
            case "Tuesday" -> 7;
            case "Thursday" -> 8;
            case "Wednesday" -> 9;
            default -> {
               System.out.println("error");
               yield 0;    // use "yield" to return a value in a block
            }
         };
      System.out.println("Number of letters: " + numLetters);
      // switch expression can also use the traditional "case L:" with yield
      day = "Wednesday";
      numLetters =
         switch (day) {
            case "Monday": case "Friday": yield 6;   // single-line expression
            case "Tuesday":   yield 7;
            case "Thursday":  yield 8;
            case "Wednesday": yield 9;
            default:
               System.out.println("error");
               yield 0;    // use "yield" to return a value in a block
         };
      System.out.println("Number of letters: " + numLetters);
   }
}
For a single-statement case-block, you can use a single expression to return a value. For blocks, you need to use yield to return a value, as shown in the above example.
Text Blocks (Preview) (tools/javac) (JEP 355)
Reference: JEP 355: Text Blocks (Preview) @ https://openjdk.java.net/jeps/355.
JDK 13 proposes to support multi-line string literal (or text block) that avoids the need for most escape sequences (such as double quote, newline), and automatically formats the multi-line string in a predictable way. This is a preview language feature.
A multi-line text block is delimited by a pair of triple double quotes, i.e., """ ... """, which may span over multiple lines.
public class TryJDK13TextBlock {
   public static void main(String[] args) {
      String html = """
                    <html>
                      <head>
                        <title>Hello</title>
                      </head>
                      <body>
                        <p>"Hello, world!"</p>
                      </body>
                    </html>
                    """;   // A multi-line text block delimited by """......"""
      System.out.println(html);
   }
}
// Compile (with preview feature) > C:\myCodeJava\jdk13_try>javac --release 13 --enable-preview TryJDK13TextBlock.java Note: TryJDK13TextBlock.java uses preview language features. Note: Recompile with -Xlint:preview for details. // Run (with preview feature) > C:\myCodeJava\jdk13_try>java --enable-preview TryJDK13TextBlock <html> <head> <title>Hello</title> </head> <body> <p>"Hello, world!"</p> </body> </html>
Take note that:
- Text block is automatically formatted, by aligning with the opening and closing """.
- To do the same thing in the traditional single-line string, you need to embed \n,\",\t, etc.; or use string concatenation'+'.
- There is no need to use escape sequence for double-quote inside a text block.
- A text block still belongs to the Stringtype.
JDK 13 Library Changes
Reimplement the Legacy Socket API (JEP 353)
Support for Unicode 12.1 (core-libs/java.util:i18n)
JDK 13 supports the Unicode 12.1. It adds 554 new characters (e.g., 61 emoji characters), and 4 new scripts.
Added FileSystems.newFileSystem(Path, Map<String, ?>) Method (core-libs/java.nio)
New java.nio.ByteBuffer Bulk get/put Methods Transfer Bytes Without Regard to Buffer Position (core-libs/java.nio)
JDK 13 Other New Features
ZGC Uncommit Unused Memory (JEP 351) (hotspot/gc)
Dynamic CDS Archiving (JEP 350) (hotspot/runtime)
REFERENCES & RESOURCES