4
Java language feature series

sequence

This article mainly describes the new features of Java 17

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

Feature list

JEP 306: Restore Always-Strict Floating-Point Semantics

Restore floating point definitions that always enforce strict mode, fix some problems with Intel's floating point instructions from 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 for 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 for 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 JDK internal apis, a follow-up to 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 preview in JDK14, as the second round preview in JDK15, and 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 was deprecated in JEP 385 in JDK15

JEP 409: Sealed Classes

Sealed Classes was introduced as a 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

To use later, you can use GraalVM

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) introduces Foreign-Memory Access API as an incubator, JDK15's JEP 383: Foreign-Memory Access API (Second Incubator) Foreign-Memory Access API as the second round of incubator, JDK16's JEP 383: Foreign-Memory Access API (Second Incubator) Foreign-Memory Access API JEP 393: Foreign-Memory Access API (Third Incubator) As the third round, it introduces Foreign Linker API, JDK17 introduces Foreign Function & Memory API

JEP 414: Vector API (Second Incubator)

JDK16 introduces JEP 338: Vector API (Incubator) provides jdk.incubator.vector for vector computation, JDK17 improves it and serves as the second round of incubator

JEP 415: Context-Specific Deserialization Filters

Allows applications to configure context-specific deserialization filters and dynamically selected deserialization filters, 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

Listed above are major features, in addition to some api updates and discards, 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 method to return the console's charset
  • JDK Flight Recorder Event for Deserialization ( JDK-8261160 )

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

    Introduced -Xlog:async parameter for asynchronous logging, and -XX:AsyncLogBufferSize=<bytes> for controlling buffer size

remove item

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

    Remove sun.misc.Unsafe::defineAnonymousClass method

obsolete

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

    Deprecated Kerberos encryption types des3-hmac-sha1 and rc4-hmac
  • Deprecate the Socket Implementation Factory Mechanism ( JDK-8235139 )

    Abandoned the following factories
     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 handling of null by the TreeMap.computeIfAbsent method deviates from the specification
  • Segmentation Fault Error on 9th and 10th Generation Intel® Core™ Processors ( JDK-8263710 )

    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 for windows is implemented in a more extensible way. The original implementation 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 )

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

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

summary

Java17 mainly has the following features

doc


codecraft
11.9k 声望2k 粉丝

当一个代码的工匠回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧,这样,当他老的时候,可以很自豪告诉世人,我曾经将代码注入生命去打造互联网的浪潮之巅,那是个很疯狂的时代,我在一波波的浪潮上留下...


引用和评论

0 条评论