0% found this document useful (0 votes)
23 views5 pages

JDK 9 New Features

Uploaded by

venkatesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views5 pages

JDK 9 New Features

Uploaded by

venkatesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1/24/2020 JDK 9 New Features

yet another insignificant programming notes...   |   HOME

TABLE OF CONTENTS (HIDE)


1.  JDK 13 New Language Features

JDK 13 (19.9) New


1.1  Switch Expression (Preview) (t
1.2  Text Blocks (Preview) (tools/j
2.  JDK 13 Library Changes
Features 3.  JDK 13 Other New Features

References:
1. "JDK 13 Release Notes", including "What's New in JDK 13" @https://www.oracle.com/technetwork/java/13-
relnote-issues-5460548.html.
2. OpenJDK's JDK 13 @ https://openjdk.java.net/projects/jdk/13/.
3. "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.

1.  JDK 13 New Language Features


JDK 13 introduces 2 new language features:
1. JEP 354: Switch Expressions (Preview)
2. JEP 355: Text Blocks (Preview)

1.1  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 break statement)

the default scoping in switch block (the whole block is treated as one scope)
switch works 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.

https://www.ntu.edu.sg/home/ehchua/programming/java/JDK13_NewFeatures.html 1/5
1/24/2020 JDK 9 New Features

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)

https://www.ntu.edu.sg/home/ehchua/programming/java/JDK13_NewFeatures.html 2/5
1/24/2020 JDK 9 New Features

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.

1.2  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>

https://www.ntu.edu.sg/home/ehchua/programming/java/JDK13_NewFeatures.html 3/5
1/24/2020 JDK 9 New Features

<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 String type.

2.  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)

3.  JDK 13 Other New Features

ZGC Uncommit Unused Memory (JEP 351) (hotspot/gc)

Dynamic CDS Archiving (JEP 350) (hotspot/runtime)

https://www.ntu.edu.sg/home/ehchua/programming/java/JDK13_NewFeatures.html 4/5
1/24/2020 JDK 9 New Features

REFERENCES & RESOURCES

Latest version tested: JDK 13.0.1


Last modified: January, 2020

Feedback, comments, corrections, and errata can be sent to Chua Hock-Chuan (ehchua@ntu.edu.sg)   |   HOME

https://www.ntu.edu.sg/home/ehchua/programming/java/JDK13_NewFeatures.html 5/5

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy