Master Java 15: Text Blocks, Records, Pattern Matching, Sealed Classes & ZGC
Explore the key Java 15 enhancements—including Text Blocks for effortless multiline strings, Records and local records for concise data models, Pattern Matching for streamlined type checks, Sealed Classes to control inheritance, and the low‑latency ZGC—plus practical IDE tips for using these features effectively.
JDK 15 Release Overview
Since JDK 9, new Java versions are released every March and September; JDK 15 arrived in September 2020. The author upgraded IntelliJ IDEA to a version that supports the latest JDK features and experimented with JDK 15.
Text Blocks Finalized
Previously, embedding HTML, XML, SQL, or JSON in Java required extensive escaping. IDEA’s "Inject Language" helped, but Java 13 introduced a preview feature called Text Blocks , allowing triple‑quoted multiline strings. The feature became a preview in JDK 14 and a standard feature in JDK 15.
Using Text Blocks eliminates manual escaping and improves readability when handling complex string literals.
Records (Second Preview)
JDK 14 added a preview record syntax for concise data classes without boilerplate getters or
toString. JDK 15 continues the preview and adds local records , enabling temporary record definitions inside methods.
<code>public record Point(int x, int y) { }</code>Example of a method using a local record to model merchant sales:
<code>List<Merchant> findTopMerchants(List<Merchant> merchants, int month) {
record MerchantSales(Merchant merchant, double sales) {}
return merchants.stream()
.map(m -> new MerchantSales(m, computeSales(m, month)))
.sorted((a, b) -> Double.compare(b.sales(), a.sales()))
.map(MerchantSales::merchant)
.collect(toList());
}</code>Local records can also be defined for enums and interfaces:
<code>// local enums
public void organisePeople(List<Person> people) {
enum Role { Employee, Customer, Both, None }
// ...
}
// local interface
public void localInterface() {
interface MyInterface { void doSomething(); }
MyInterface test = new MyInterface() { @Override public void doSomething() { System.out.println("Hello World!"); } };
// ...
}</code>Local records, enums, and interfaces are scoped to the enclosing method and cannot be referenced elsewhere.
Pattern Matching for instanceof (Second Preview)
Traditional
instanceofchecks require a cast:
<code>if (obj instanceof String) {
String str = (String) obj;
// use str
}</code>The new pattern‑matching syntax combines the test and cast:
<code>if (obj instanceof String s) {
s.contains("T");
} else {
// compile‑time error if used incorrectly
}</code>IDEA can suggest converting existing code to this pattern.
Sealed Classes (Preview)
Java traditionally allowed any class to extend another, or used
finalto prevent inheritance entirely. Sealed classes let developers specify exactly which classes may extend a superclass.
<code>public sealed class Shape permits Circle, Rectangle, Square { ... }</code>Subclasses must be declared
final,
sealed, or
non‑sealedto control further extension:
<code>public final class Circle extends Shape { ... }
public sealed class Rectangle extends Shape permits TransparentRectangle, FilledRectangle { ... }
public final class TransparentRectangle extends Rectangle { ... }</code>Sealed classes can also be used with records to model algebraic data types.
ZGC (Z Garbage Collector)
ZGC, introduced experimentally in JDK 11, aims for sub‑10 ms pause times regardless of heap size. After several iterations, it became a standard collector in JDK 15. To enable it, add the JVM flag:
<code>-XX:+UseZGC</code>Conclusion
The article originally intended to cover new IDEA 2020.2 features but expanded into a dedicated overview of JDK 15 enhancements, highlighting how they simplify code and improve performance.
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
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.