New features of Java17

codecraft
中文
Java language feature series

sequence

This article mainly talks about the new features of Java17

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)
It can be seen from the version information that it is build 17+35

Features list

JEP 306: Restore Always-Strict Floating-Point Semantics

Restore the floating-point definition that always implements strict mode, and fix some problems with Intel's floating-point instructions 25 years ago

JEP 356: Enhanced Pseudo-Random Number Generators

Introduce RandomGenerator and RandomGeneratorFactory 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();

JEP 382: New macOS Rendering Pipeline

Added Java 2D internal rendering pipeline to macOS using Apple Metal API

JEP 391: macOS/AArch64 Port

Migrate JDK to macOS/AArch64

JEP 398: Deprecate the Applet API for Removal

Mark the Applet API as obsolete to facilitate subsequent removal, as follows
java.applet.Applet
java.applet.AppletStub
java.applet.AppletContext
java.applet.AudioClip
javax.swing.JApplet
java.beans.AppletInitializer

JEP 403: Strongly Encapsulate JDK Internals

Stronger encapsulation of the JDK internal api, which is the follow-up JEP 396: Strongly Encapsulate JDK Internals by Default

JEP 406: Pattern Matching for switch (Preview)

Introduce the preview version of switch pattern matching. The pattern matching of instanceof is used as a preview in JDK14, as a second round of preview in JDK15, and it is converted to positive in JDK16.
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();
    };
}

JEP 407: Remove RMI Activation

Remove Remote Method Invocation (RMI), which is obsoleted JEP 385

JEP 409: Sealed Classes

Sealed Classes were introduced as preview in JDK15, as the second round of preview in JDK16, and turned positive 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 { ... }

JEP 410: Remove the Experimental AOT and JIT Compiler

Remove the experimental java version of AOT and JIT Compiler, specifically remove
jdk.aot — the jaotc tool
jdk.internal.vm.compiler — the Graal compiler
jdk.internal.vm.compiler.management — Graal's MBean

GraalVM can be used for subsequent use

JEP 411: Deprecate the Security Manager for Removal

Abandon the Security Manager introduced by java1.0 to facilitate subsequent removal

JEP 412: Foreign Function & Memory API (Incubator)

JDK14’s JEP 370: Foreign-Memory Access API (Incubator) introduced the Foreign-Memory Access API as an incubator, and JDK15’s JEP 383: Foreign-Memory Access API (Second Incubator) Foreign-Memory Access API as the second round Incubator, JDK16 JEP 393: Foreign-Memory Access API (Third Incubator) as the third round, it introduced Foreign Linker API, JDK17 introduced Foreign Function & Memory API

JEP 414: Vector API (Second Incubator)

JDK16 introduced JEP 338: Vector API (Incubator) provided jdk.incubator.vector for vector calculation, JDK17 was improved and used as the second round of incubator

JEP 415: Context-Specific Deserialization Filters

Allows the application to configure the deserialization filters of the specified context and dynamic selection, example
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);
    });

Detailed interpretation

The features listed above are general features, in addition to some API updates and obsolescence, 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 adds a new method to return the charset of the console
  • JDK Flight Recorder Event for Deserialization (JDK-8261160)

    JDK Flight Recorder adds jfr.Deserialization implementation
  • Unified Logging Supports Asynchronous Log Flushing (JDK-8229517)

    The -Xlog:async parameter is introduced for asynchronous logging, and -XX:AsyncLogBufferSize=<bytes> used to control the size of the buffer

Remove item

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

    Remove sun.misc.Unsafe::defineAnonymousClass method

Obsolete item

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

    The two encryption types des3-hmac-sha1 and rc4-hmac of Kerberos are abandoned
  • Deprecate the Socket Implementation Factory Mechanism (JDK-8235139)

    The following factories were abandoned
    static void ServerSocket.setSocketFactory​(SocketImplFactory fac)
    static void Socket.setSocketImplFactory​(SocketImplFactory fac)
    static void DatagramSocket.setDatagramSocketImplFactory​(DatagramSocketImplFactory fac)

Known issues

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

    The TreeMap.computeIfAbsent method for null processing is deviated from the specification
  • Segmentation Fault Error on 9th and 10th Generation Intel® Core™ Processors (JDK-8263710)

    There will be Segmentation Fault Error when running on 9th and 10th Generation Intel® Core™ Processors

something else

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

    xml-commons-api, jaxp_parser_impl, and java-fonts have been removed from OracleJDK/OracleJRE RPMs
  • New Implementation of java.nio.channels.Selector on Microsoft Windows (JDK-8266369)

    The java.nio.channels.Selector API of windows is implemented in a more extensible way. The original implementation has not been removed. You can continue to use -Djava.nio.channels.spi.SelectorProvider=sun.nio.ch.WindowsSelectorProvider
  • Parallel GC Enables Adaptive Parallel Reference Processing by Default (JDK-8204686)

    -XX:ParallelRefProcEnabled turned on by default for Parallel GC
  • URLClassLoader No Longer Throws Undocumented IllegalArgumentException From getResources and findResources (JDK-8262277)

    URLClassLoader's getResources and findResources no longer throw IllegalArgumentException which is not defined by the document

summary

Java17 mainly has the following features

doc

阅读 2.9k

code-craft
spring boot , docker and so on 欢迎关注微信公众号: geek_luandun

当一个代码的工匠回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧,这样,当他老的时候,可以很...

11.6k 声望
1.9k 粉丝
0 条评论
你知道吗?

当一个代码的工匠回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧,这样,当他老的时候,可以很...

11.6k 声望
1.9k 粉丝
文章目录
宣传栏