JDK 9 New Features
JDK 9 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/.
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.
https://www.ntu.edu.sg/home/ehchua/programming/java/JDK13_NewFeatures.html 1/5
1/24/2020 JDK 9 New Features
For example:
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:
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,
// 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.
A multi-line text block is delimited by a pair of triple double quotes, i.e., """ ... """, which may span over
multiple lines.
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);
}
}
https://www.ntu.edu.sg/home/ehchua/programming/java/JDK13_NewFeatures.html 4/5
1/24/2020 JDK 9 New Features
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