JDK 20 Release! - Sip of Java

JDK 20 was released on March 21st! This is the 11th iteration of the JDK being released on the six-month release schedule that started with JDK 10 in March of 2018. JDK 20 included seven new JEPs, JDK enhancement proposals, all in incubator or preview status. Let’s review these JEPs and what they mean for the future of Java!

Project Loom

Project Loom had three JEPs included in JDK 20;

JEP 429 Scoped Values (Incubator)

JEP 429, Scoped Values, is the most significant change introduced in JDK 20. Scoped Values represent the final of the initial trifecta of major changes being introduced by Project Loom, the others being Virtual Threads and Structured Concurrency. Scoped Values provide an immutable way to share data within and across Virtual Threads. Scoped Values seek to improve upon some of the shortcomings of Thread Local, which would be particularly noticeable in the context of Virtual Threads where there might be thousands of threads present. Check the additional reading section for a link to José Paumard’s video on Scoped Values for a deeper dive into this feature.

JEP 436 Virtual Threads (Second Preview)

Virtual Threads enters second preview with JEP 436. No new API changes were introduced with JEP 436, and several APIs introduced in JDK 19 were finalized as they were useful outside of the context of virtual threads. The APIs that have been finalized were:

  • Thread.join(Duration), sleep(Duration), threadId().
  • Future.resultNow(), exceptionNow(), state().
  • ExecutorService is Autocloseable.

JEP 437 Structured Concurrency (Second Incubator)

Structured Concurrency enters second incubator in JDK 20 with JEP 436. There are no new API changes with JEP 437; the only change is that StructuredTaskScope has been updated to support the inheritance of Scoped Values.

Project Amber

Project Amber had two JEPs included in JDK 20;

JEP 432 Record Patterns (Second Preview)

Record Patterns is in second preview with JEP 432 and includes three key changes.

  • Added support for type inferencing of generic record patterns:
interface Name<T> {}
record FullName<T>(T firstName, T lastName) implements Name<String> {};
	
public static void main(String... args){
	FullName <String> name = new FullName<>("William", "Korando");
	printName(name);
}
	
public static void printName(Name name) {
    	if(name instanceof FullName(var first, var last))
        	System.out.println(last + ", " + first);
}
  • Added support for record patterns in enhanced for loop headers:
record Point(int x, int y) {}

static void dump(Point[] pointArray) {
    for (Point(var x, var y) : pointArray) { 
        System.out.println("(" + x + ", " + y + ")");
    }
}
  • Removed support for named record patterns:
Object object = new Point(1, 2);

if (object instanceof Point(int i, int j) p) {
    System.out.println("object is a Point, p.i = " 
    	+ p.i() + ", p.j = " + p.j()
        + ", i = " + i + ", j = " + j);
}

JEP 433 Pattern Matching for Switch (Fourth Preview)

Pattern Matching for Switch entered fourth, and hopefully final, preview status with JEP 433. This round of preview included three changes:

  • Exhaustive switch will throw MatchException instead of IncompatibleClassChangeError.
  • Simplification of the switch label.
  • Support for type inferencing of record patterns in switch labels.

Project Panama

Project Panama had two JEPs included in JDK 20;

JEP 434 Foreign Function & Memory API (Second Incubator)

JEP 434 defined the second incubator for the Foreign Function & Memory API. There were three API changes included in this JEP:

  • The MemorySegment and MemoryAddress abstractions are unified.
  • The sealed MemoryLayout hierarchy is enhanced to facilitate usage with pattern matching in switch.
  • MemorySession has been split into Arena and SegmentScope.

JEP 438 Vector API (Fifth Incubator)

JEP 438, fifth incubator for Vector API, was added late in the release process for JDK 20. This resulted from miscommunication, as no API changes were made to the Vector API in JDK 20; however, features in incubator status still require a JEP for every release they are included in while in incubator status. JEP 438 clarifies that Vector API will remain in incubator status until features from Project Valhalla, specifically value classes, are delivered.

Additional Reading

Java 20 - From ThreadLocal to ScopedValue with Loom Full Tutorial - https://www.youtube.com/watch?v=fjvGzBFmyhM

Happy coding!