JDK 22 in Two Minutes! - Sip of Java
Billy Korando on March 21, 2024In this article, we will take a brief look at the JEPs, or JDK Enhancement Proposals, that are part of JDK22!
Final Feature JEPs
Four JEPs included final features.
JEP 423 added Region Pinning for G1. This allows G1 to still perform garbage collection on regions with objects being used by JNI (Java Native Interface) by pinning those objects in that region.
JEP 454 finalizes the Foreign Function and Memory API (FFM API). The FFM API is an improvement over JNI, by enhancing developer productivity and experience, performance, security, and consistency when working with native libraries.
JEP 456 finalizes Unnamed Variables and Patterns. This JEP allows variables and pattern variables you do not intend to use to be replaced with an underscore. This not only reduces code verbosity but also improves the intent of the code.
sealed abstract class Ball
permits RedBall, BlueBall, GreenBall { }
final class RedBall extends Ball { }
final class BlueBall extends Ball { }
final class GreenBall extends Ball { }
Ball ball = ...
switch (ball) {
case RedBall _ -> process(ball);
case BlueBall _ -> process(ball);
case GreenBall _ -> stopProcessing();
}
JEP 458, Launch Multi-File Source-Code Programs, as the name suggests, allows the Java launcher to compile and launch multiple source code files, an improvement over JEP 330 Launch Single-File Source-Code Programs
// file MainApplication.java
public class MainApplication {
public static void main(String[] args) {
Person p = new Person("Billy", "Korando");
System.out.println("Hello, " + p.toString() + "!");
}
}
// file Person.java
record Person(String fName, String lName) {
public String toString(){
return fName + " " + lName;
}
}
$ java MainApplication.java
Hello Billy Korando!
Preview Features JEPs
JEP 447 allows statements to be added before a call to super in constructors. This allows developers to add validation or other processing to be done to variables before calling a parent constructor. Like in this code example:
public class PositiveBigInteger extends BigInteger {
public PositiveBigInteger(long value) {
if (value <= 0)
throw new IllegalArgumentException("non-positive value");
super(value);
}
}
JEP 457 provides a standard API for class-files. It’s unlikely most developers will interact with this directly, but it will improve the experience of upgrading between JDK versions.
JEP 461 introduces stream gatherers. Stream gatherers are an intermediate operation that can be applied to streams to transform elements within the stream.
JEP 463 Implicitly Declared Classes and Instance Main Methods is the second preview of JEP 445, Unnamed Classes and Instance Main Methods. It includes rule changes to implicitly declare classes in source files and procedures for selecting which main method to call.
void main(){
System.out.println("Hello World!");
}
JEPs 459, 462, and 464, String templates, Structured concurrency, and scoped values, respectively, enter second preview with JEPs, all containing either minor or no API changes. However, Brian Goetz recently announced a major revision for String Template . Be sure to check here on inside.java and the Java YouTube channel for updates!
Incubator Feature JEPs
JEP 460, the Vector API, is the only incubator feature with its seventh iteration of incubator status.
The Vector API will continue to remain in incubator status pending the release of project Valhalla features.
Additional Reading
JDK 22 Project page - https://openjdk.org/projects/jdk/22/
Update on String Templates (JEP 459) - https://mail.openjdk.org/pipermail/amber-spec-experts/2024-March/004010.html
Happy coding!