Java language features series

  • New features in Java5
  • New features for Java6
  • New features in Java7
  • New features in Java8
  • New features in Java9
  • New features in Java10
  • New features for Java11
  • New features in Java12
  • New features for Java13
  • New features for Java14
  • New features for Java15
  • New features for Java16
  • New features for Java17
  • New features for Java18

sequence

This article focuses on the new features of Java17

The version number

java -version
openjdk version "17" 2021-09-14
OpenJDK Runtime Environment (build 17+35-2724)
OpenJDK 64-Bit Server VM (build 17+35-2724, mixed mode, sharing)
Copy the code

The version information shows that it is Build 17+35

Feature list

JEP 306: Restore Always-Strict Floating-Point Semantics

Restore floating point definitions that always enforce strict patterns and fix some problems with Intel floating point instructions from 25 years ago

JEP 356: Enhanced Pseudo-Random Number Generators

RandomGenerator and RandomGeneratorFactory are introduced to provide better random number generation

RandomGenerator generator = RandomGeneratorFactory.all()
    .filter(RandomGeneratorFactory::isJumpable)
    .filter(factory -> factory.stateBits() > 128)
    .findAny()
    .map(RandomGeneratorFactory::create)
//  if you need a `JumpableGenerator`:
//  .map(JumpableGenerator.class::cast)
    .orElseThrow();
Copy the code

JEP 382: New macOS Rendering Pipeline

Added Java 2D Internal Rendering Pipeline for macOS using Apple Metal API

JEP 391: macOS/AArch64 Port

Migrate the JDK to macOS/AArch64

JEP 398: Deprecate the Applet API for Removal

Mark the Applet API as obsolete for subsequent removal, as shown below

java.applet.Applet
java.applet.AppletStub
java.applet.AppletContext
java.applet.AudioClip
javax.swing.JApplet
java.beans.AppletInitializer
Copy the code

JEP 403: Strongly Encapsulate JDK Internals

This is a follow-up to JEP 396: Strongly Encapsulate JDK Internals by Default

JEP 406: Pattern Matching for switch (Preview)

Switch pattern matching is introduced in the Preview version, instanceof pattern matching in JDK14 as preview, in JDK15 as second preview, and in JDK16 as normal

static String formatterPatternSwitch(Object o) {
    return switch (o) {
        case Integer i -> String.format("int %d", i);
        case Long l    -> String.format("long %d", l);
        case Double d  -> String.format("double %f", d);
        case String s  -> String.format("String %s", s);
        default        -> o.toString();
    };
}
Copy the code

JEP 407: Remove RMI Activation

Remove Remote Method Invocation (RMI), it is deprecated on JEP 385 in JDK15

JEP 409: Sealed Classes

Sealed Classes is introduced in JDK15 as a preview, in JDK16 as a second preview, and converted in JDK17

package com.example.geometry;

public abstract sealed class Shape
    permits Circle, Rectangle, Square, WeirdShape { ... }

public final class Circle extends Shape { ... }

public sealed class Rectangle extends Shape 
    permits TransparentRectangle, FilledRectangle { ... }
public final class TransparentRectangle extends Rectangle { ... }
public final class FilledRectangle extends Rectangle { ... }

public final class Square extends Shape { ... }

public non-sealed class WeirdShape extends Shape { ... }
Copy the code

JEP 410: Remove the Experimental AOT and JIT Compiler

Removed experimental Java versions of AOT and JIT Compiler, specifically removed

Piler JDK. - the aot jaotc tool jdk.internal.vm.com - the Graal compiler jdk.internal.vm.com piler. Management - Graal 's MBeanCopy the code

GraalVM can be used for subsequent use

JEP 411: Deprecate the Security Manager for Removal

Deprecate the Security Manager introduced in java1.0 for subsequent removal

JEP 412: Foreign Function & Memory API (Incubator)

JEP 370: Foreign-memory Access API (Incubator) Introduction of foreign-memory Access API as Incubator, JEP 383 of JDK15: Foreign-memory Access API (Second Incubator) As the Second round, JEP 393 of JDK16: Foreign-memory Access API (Third Incubator) As the Third round, it introduces Foreign Linker API and JDK17 introduces Foreign Function & Memory API

JEP 414: Vector API (Second Incubator)

JDK16 introduced the JEP 338: Vector API (Incubator) to provide JDk.incubator. Vector for Vector computation, and JDK17 improved and served as the Incubator for the second round

JEP 415: Context-Specific Deserialization Filters

Example of deserialization filters that allow applications to configure specified context and dynamic selection

public class FilterInThread implements BinaryOperator<ObjectInputFilter> { // ThreadLocal to hold the serial filter to be applied private final ThreadLocal<ObjectInputFilter> filterThreadLocal = new ThreadLocal<>(); // Construct a FilterInThread deserialization filter factory. public FilterInThread() {} /** * The filter factory, which is invoked every time a new ObjectInputStream * is created. If a per-stream filter is already set then it returns a * filter that combines the results of invoking each filter. * * @param curr the current filter on the stream * @param next a per stream filter * @return the selected filter */ public ObjectInputFilter apply(ObjectInputFilter curr, ObjectInputFilter next) { if (curr == null) { // Called from the OIS constructor or perhaps OIS.setObjectInputFilter with no current filter var filter = filterThreadLocal.get(); if (filter ! = null) { // Prepend a filter to assert that all classes have been Allowed or Rejected filter = ObjectInputFilter.Config.rejectUndecidedClass(filter); } if (next ! = null) { // Prepend the next filter to the thread filter, if any // Initially this is the static JVM-wide filter passed from the OIS constructor // Append the filter to reject all UNDECIDED results filter = ObjectInputFilter.Config.merge(next, filter); filter = ObjectInputFilter.Config.rejectUndecidedClass(filter); } return filter; } else { // Called from OIS.setObjectInputFilter with a current filter and a stream-specific filter. // The curr filter already incorporates the thread filter and static JVM-wide filter // and rejection of undecided classes // If there is a  stream-specific filter prepend it and a filter to recheck for undecided if (next ! = null) { next = ObjectInputFilter.Config.merge(next, curr); next = ObjectInputFilter.Config.rejectUndecidedClass(next); return next; } return curr; } } /** * Apply the filter and invoke the runnable. * * @param filter the serial filter to apply to every deserialization in the thread * @param runnable a Runnable to invoke */ public void doWithSerialFilter(ObjectInputFilter  filter, Runnable runnable) { var prevFilter = filterThreadLocal.get(); try { filterThreadLocal.set(filter); runnable.run(); } finally { filterThreadLocal.set(prevFilter); } } } // Create a FilterInThread filter factory and set var filterInThread = new FilterInThread(); ObjectInputFilter.Config.setSerialFilterFactory(filterInThread); // Create a filter to allow example.* classes and reject all others var filter = ObjectInputFilter.Config.createFilter("example.*; java.base/*; ! * "); filterInThread.doWithSerialFilter(filter, () -> { byte[] bytes = ... ; var o = deserializeObject(bytes); });Copy the code

Details of interpretation

In addition to the major features listed above, there are also some API updates and deprecations, mainly see JDK 17 Release Notes, here are a few examples.

Add item

  • DatagramSocket Can Be Used Directly to Join Multicast Groups (JDK-8237352)

Updated java.net.DatagramSocket to support Joining multicast groups

  • Console Charset API (JDK-8264208)

Java.io.Console added a method to return the Console charset

  • JDK Flight Recorder Event for Deserialization (JDK-8261160)

Jfr. Deserialization implementation was added to JDK Flight Recorder

  • Unified Logging Supports Asynchronous Log Flushing (JDK-8229517)

-xlog :async parameters were introduced for asynchronous logging, and -xx :AsyncLogBufferSize=

to control the size of buffers

Remove items

  • Removal of sun.misc.Unsafe::defineAnonymousClass (JDK-8243287)

Remove sun. Misc. Unsafe: : defineAnonymousClass method

Discarded items

  • Deprecate 3DES and RC4 in Kerberos (JDK-8139348)

Two encryption types of Kerberos des3-HMAC-SHA1 and RC4-HMAC are deprecated

  • Deprecate the Socket Implementation Factory Mechanism (JDK-8235139)

The following factories have been abandoned

static void ServerSocket.setSocketFactory​(SocketImplFactory fac)
static void Socket.setSocketImplFactory​(SocketImplFactory fac)
static void DatagramSocket.setDatagramSocketImplFactory​(DatagramSocketImplFactory fac)
Copy the code

Known issues

  • TreeMap.computeIfAbsent Mishandles Existing Entries Whose Values Are null (JDK-8259622)

The TreeMap.computeIfAbsent method does not handle NULL correctly

  • Segmentation Fault Error on 9th and 10th Generation Intel® Core™ Processors (JDK-8263710)

A Segmentation Fault Error occurs when running 9th and 10th Generation Intel® Core™ Processors

Other matters

  • Updated List of Capabilities Provided by JDK RPMs (JDK-8263575)

Xml-commons-api, JAXP_Parser_IMPl, and Java-fonts have been removed from the OracleJDK/OracleJRE RPMs

  • New Implementation of java.nio.channels.Selector on Microsoft Windows (JDK-8266369)

Java for Windows. Nio. Channels. The Selector API USES more scalable ways, the realization of the original has not been removed, You can use – Djava. Nio. Channels. Spi. SelectorProvider = sun. Nio. Ch. WindowsSelectorProvider to continue to use

  • Parallel GC Enables Adaptive Parallel Reference Processing by Default (JDK-8204686)

For the Parallel GC opened by default – XX: ParallelRefProcEnabled

  • URLClassLoader No Longer Throws Undocumented IllegalArgumentException From getResources and findResources (JDK-8262277)

The URLClassLoader’s getResources and findResources no longer throw illegalArgumentExceptions that are not defined by the document

summary

Java17 has the following features

  • JEP 306: Restore Always-Strict Floating-Point Semantics
  • JEP 356: Enhanced Pseudo-Random Number Generators
  • JEP 382: New macOS Rendering Pipeline
  • JEP 391: macOS/AArch64 Port
  • JEP 398: Deprecate the Applet API for Removal
  • JEP 403: Strongly Encapsulate JDK Internals
  • JEP 406: Pattern Matching for switch (Preview)
  • JEP 407: Remove RMI Activation
  • JEP 409: Sealed Classes
  • JEP 410: Remove the Experimental AOT and JIT Compiler
  • JEP 411: Deprecate the Security Manager for Removal
  • JEP 412: Foreign Function & Memory API (Incubator)
  • JEP 414: Vector API (Second Incubator)
  • JEP 415: Context-Specific Deserialization Filters

doc

  • JDK 17 Features
  • JDK 17 Release Notes
  • JDK 17 Release Notes
  • Java SE deprecated-list
  • The arrival of Java 17!
  • Java 17 and IntelliJ IDEA
  • Better Random Number Generation in Java 17