Author: Karsten Silz Mar 22, 2021 3 min read

Permalink: https://betterprojectsfaster.com/blog/release-jdk-16/

JDK 16: Records, Patterns, Ports & Garbage Collectors

Number 16

What’s This About?

Another half a year has gone by, so here we go: Today, Oracle released JDK 16 or “Next Java LTS, final beta”. That release comes half a year after JDK 15. And JDK 17 will finally be here in September!

What’s in the Release?

Just like last time, Tech Geek Next has a good overview. So, what affects us Java developers day-in, day-out?

Records

First stop is probably records. They are “immutable value classes” and leave the preview status behind with Java 16. That’s the equivalence of Project Lombok’s @Value classes. So in Java 16, a record Point(int x, int y) { } turns into this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Point {
  private final int x;
  private final int y;

  Point(int x, int y) {
     this.x = x;
     this.y = y;
  }

  int x() { return x; }
  int y() { return y; }

  public boolean equals(Object o) {
    if (!(o instanceof Point)) return false;
    Point other = (Point) o;
    return other.x == x && other.y == y;
  }

  public int hashCode() {
    return Objects.hash(x, y);
  }

  public String toString() {
    return String.format("Point[x=%d, y=%d]", x, y);
  }
}

As I mentioned in last week’s issue of my newsletter, record serialization is easier to understand, has no magic, is more secure, and offers better performance. That’s a nice bonus over Lombok!

Pattern Matching

We often write this code:

1
2
3
4
if (obj instanceof String) {
    String s = (String) obj;    // grr...
    ...
}

With Java 16, we can now move the variable declaration of s into the if condition:

1
2
3
if (obj instanceof String s) {
    ...
}

It’s not earthshaking, but it does save a few keystrokes.

OpenJDK Ports

OpenJDK is now available on Windows ARM64. What’s that? Well, it’s like Apple M1-powered MacBooks, but worse. So if you’re one of the few people who has a Windows on ARM device (like the Microsoft Surface Pro X, then you can now run Java natively on it!

OpenJDK is now also out on Alpine Linux. Alpine Linux the smallest Linux distribution with wide circulation. So this means that our Docker images can get smaller!

Garbage Collectors

Some customer, especially in the financial industry, want very small garbage collection pauses in Java. So they look alternate JDK distributions with specialized garbage collectors, like Azul’s Zing. Now in JDK 16, the ZGC garbage collector claims “pauses below a single millisecond”. And G1 is faster, too, shaving off possibly 50~100ms+ in some cases. This may get some folks to look at the regular JDK 16 again!

Should I Upgrade?

No. Wait for Java 17.

If you’re on the bleeding edge and need this release, you know it and don’t need my advice.

Still, Where Can I Get It?

The popular OpenJDK distributor AdoptOpenJDK has Java 16. As usual, get the HotSpot version, unless you’re sure that you need the OpenJ9 version.

When Will the Next JDK LTS Version Be Out?

JDK 17 will be out in September 2021. It’s the next release!

Sign up to my weekly newsletter
on how to build Java applications today!
release  jdk 


comments powered by Disqus