Explore Java 14: New Language Features and How to Use Them
This article introduces the JDK 14 release, lists its sixteen new features—including pattern‑matching instanceof, switch expressions, record types, and text blocks—explains required IDE support, and provides concrete code examples and usage guidelines for each feature.
JDK 14 was officially released on 2020‑03‑17. Although it is not a long‑term support version, it builds on previous releases and requires a compatible IDE such as IntelliJ IDEA 2020.1 to recognize the new language constructs.
Java 14 New Features
JDK 14 adds sixteen new features:
JEP 305: Pattern Matching for instanceof (preview)
JEP 343: jpackage packaging tool (Incubator)
JEP 345: NUMA‑aware G1 memory allocation
JEP 349: JFR event streaming
JEP 352: Non‑atomic byte‑buffer mapping
JEP 358: Helpful NullPointerException JEP 359: Records (preview)
JEP 361: Switch expressions (standard)
JEP 362: Deprecate Solaris and SPARC ports
JEP 363: Remove the CMS garbage collector
JEP 364: ZGC on macOS
JEP 365: ZGC on Windows
JEP 366: Deprecate the ParallelScavenge + SerialOld GC combination
JEP 367: Remove Pack200 tools and API
JEP 368: Text blocks (second preview)
JEP 370: Foreign‑memory access API (Incubator)
Features 363, 364, 365, and 366 relate to garbage‑collector changes; notably, CMS is deprecated.
Pattern Matching for instanceof
Traditional code before Java 14:
Object obj = "程序新视界";
if (obj instanceof String) {
String str = (String) obj;
System.out.println("关注公众号:" + str);
}With the preview pattern‑matching feature:
Object obj = "程序新视界";
if (obj instanceof String str) {
System.out.println("关注公众号:" + str);
}Switch Expressions
Classic switch statement:
switch (day) {
case MONDAY:
case FRIDAY:
case SUNDAY:
System.out.println(6);
break;
case TUESDAY:
System.out.println(7);
break;
case THURSDAY:
case SATURDAY:
System.out.println(8);
break;
case WEDNESDAY:
System.out.println(9);
break;
}Java 14 introduces arrow‑label syntax, allowing multiple constants per case and eliminating the need for break:
switch (day) {
case MONDAY, FRIDAY, SUNDAY -> System.out.println(6);
case TUESDAY -> System.out.println(7);
case THURSDAY, SATURDAY -> System.out.println(8);
case WEDNESDAY -> System.out.println(9);
}Record Types (preview)
Records provide a compact syntax for immutable data carriers.
public record Point(int x, int y) { }Usage example:
Point point = new Point(1, 3);
System.out.println(point.x());
System.out.println(point.y());Decompiling the record shows a final class extending java.lang.Record with private final fields, a canonical constructor, accessor methods, and overridden toString, hashCode, and equals implementations.
Text Blocks (preview)
Long string literals can be expressed with triple‑quoted text blocks, preserving line breaks and quotes without explicit concatenation.
String html = """
<html>
<body>
<p>Hello, world</p>
</body>
</html>
""";Text blocks improve readability and reduce escaping overhead.
jpackage Tool (Incubator)
The jpackage utility can create native installers for Java applications. Supported package formats are:
Linux: deb and rpm macOS: pkg and dmg Windows: msi and
exeConclusion
Java 14 also includes other optimizations such as deprecating the ParallelScavenge + SerialOld GC combination. Because many features are preview or incubator, they should be evaluated carefully before adopting them in production environments.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Senior Brother's Insights
A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
