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
  • New features for Java19

sequence

This article focuses on the new features of Java18

The version number

java -version
openjdk version "18" 2022-03-22
OpenJDK Runtime Environment (build 18+36-2087)
OpenJDK 64-Bit Server VM (build 18+36-2087, mixed mode, sharing)
Copy the code

The version information shows that it is Build 18+36

Feature list

JEP 400: UTF-8 by Default

Before java18, charset.defaultcharset () was determined by operating system, user locale, etc., resulting in different operating system defaultCharset. Encoding = utF-8 -dfile. encoding= utF-8 -dfile. encoding=COMPAT -dfile. encoding=COMPAT

JEP 408: Simple Web Server

An out-of-the-box HTTP file server that provides a Python-like SimpleHTTPServer(python -m SimpleHTTPServer [port]) can be started with jwebServer -P 9000

Jwebserver -p 9000 Binding to loopback by default. For all interfaces use "-b 0.0.0.0" or "-b ::". Serving/TMP and Subdirectories on 127.0.0.1 port 9000 URL http://127.0.0.1:9000/Copy the code

It can also be customized and launched in code

jshell> var server = SimpleFileServer.createFileServer(new InetSocketAddress(8080), ... > Path.of("/some/path"), OutputLevel.VERBOSE); jshell> server.start()Copy the code

JEP 413: Code Snippets in Java API Documentation

Previously, to display code through Javadoc, you could use @code as follows

<pre>{@code
    lines of source code
}</pre>
Copy the code

The downside is that it’s pre wrapped, so the segment doesn’t contain HTML tags, and indentation isn’t flexible

This time, the @snippet tag is introduced to javaDoc without escaping HTML tags

/** * The following code shows how to use {@code Optional.isPresent}: * {@snippet : * if (v.isPresent()) { * System.out.println("v: " + v.get()); *} *} */Copy the code

You can also refer directly to the source code to prevent the Javadoc code from disconnecting from the actual code

/**
 * The following code shows how to use {@code Optional.isPresent}:
 * {@snippet file="ShowOptional.java" region="example"}
 */
Copy the code

JEP 416: Reimplement Core Reflection with Method Handles

Through the Method Handles to implement Java. Lang. Reflect the Method, Constructor and Field to replace the bytecode generation Method: : invoke, Constructor: : newInstance, The implementation of Field:: GET, and Field::set is convenient to support Project Valhalla and reduce the cost of expansion in the future

JEP 417: Vector API (Third Incubator)

JDK16 introduces JEP 338: Vector API (Incubator) to provide JDk.incubator. Vector for Vector computation JDK17 to improve and serve as a second round of incubatorJEP 414: Vector API (Second Incubator) JDK18 was improved and used as the Incubator of the third round

JEP 418: Internet-Address Resolution SPI

Provide analytical network address with SPI, namely java.net.spi package InetAddressResolverProvider convenient to project loom preparation (currently InetAddress API will be blocked in system calls), also facilitate customization, and testing

JEP 419: Foreign Function & Memory API (Second 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 of Incubator JDK16 JEP 393: Foreign-memory Access API (Third Incubator) As the Third round, it introduces the Foreign Linker API JDK17 into JEP 412: Foreign Function & Memory API (Incubator) was used as the first round of Incubator JDK18 was used as the second round of Incubator

JEP 420: Pattern Matching for switch (Second Preview)

The instanceof pattern matches 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

JDK17 introduces JEP 406: Pattern Matching for Switch (Preview) JDK18 is the second round of Preview

JEP 421: Deprecate Finalization for Removal

Discarding Finalize method is convenient for subsequent removal

Details of interpretation

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

Add item

  • SerialGC, ParallelGC, and ZGC support String Deduplication

To enable this function, run the -xx :+UseStringDeduplication command

  • Map from an Element to its JavaFileObject

Added Elements. GetFileObjectOf (Element) to map to JavaFileObject

  • Configurable Card Table Card SizeJDK-8272773

You can use -xx :GCCardSizeInBytes to set the card table size

  • Allow G1 Heap Regions up to 512MBJDK-8275056

The maximum heap regions for G1 is allowed to increase from 32MB to 512MB

  • JDK Flight Recorder Event for FinalizationJDK-8266936

The new JDK. FinalizerStatistics

Remove items

  • Removal of Google’s GlobalSign Root CertificateJDK-8225083

Removed Google’s GlobalSign root certificate

  • Removal of Empty finalize() Methods in java.desktop ModuleJDK-8273102

Remove the empty Finalize () method from the java.desktop module

  • Removal of impl.prefix JDK System Property Usage From InetAddressJDK-8274227

Remove the impl. Prefix attribute and use the spi InetAddressResolver instead

  • Removal of Support for Pre JDK 1.4 DatagramSocketImpl ImplementationsJDK-8260428

Remove DatagramSocketImpl prior to jdk1.4

  • Removal of Legacy PlainSocketImpl and PlainDatagramSocketImpl ImplementationsJDK-8253119

Remove old PlainSocketImpl, PlainDatagramSocketImpl java.net.SocketImpl and java.net.DatagramSocketImpl Jdk.net.usePlainDatagramSocketImpl properties was also removed

Discarded items

See Deprecated -list for a complete list

  • Deprecated Subject::doAs for RemovalJDK-8267108

Abandoned javax.mail. Security. Auth. Subject: : doAs in preparation for the removal

  • Deprecated sun.misc.Unsafe Methods That Return OffsetsJDK-8277863

The objectFieldOffset, staticFieldOffset, and staticFieldBase methods in sun.misc.Unsafe are discarded

  • Terminally Deprecated Thread.stopJDK-8277861

Disable Thread.stop in preparation for subsequent removal

  • Obsoleted Product Options -XX:G1RSetRegionEntries and -XX:G1RSetSparseRegionEntriesJDK-8017163

Abandoned – XX: G1RSetRegionEntries and – XX: G1RSetSparseRegionEntries

Known issues

  • Extended Delay Before JDK Executable Installer Starts From Network DriveJDK-8274002

On Windows 11 and Windows Server 2022, extraction of temporary installation files may be somewhat slow when booting from a mapped network drive. Setup will still work, but there may be a temporary delay.

summary

Java18 has the following features

  • JEP 400: UTF-8 by Default
  • JEP 408: Simple Web Server
  • JEP 413: Code Snippets in Java API Documentation
  • JEP 416: Reimplement Core Reflection with Method Handles
  • JEP 417: Vector API (Third Incubator)
  • JEP 418: Internet-Address Resolution SPI
  • JEP 419: Foreign Function & Memory API (Second Incubator)
  • JEP 420: Pattern Matching for switch (Second Preview)
  • JEP 421: Deprecate Finalization for Removal

doc

  • JDK 18 Features
  • JDK 18 Release Notes
  • Consolidated JDK 18 Release Notes
  • Java SE 18 deprecated-list
  • The Arrival of Java 18
  • JDK 18 G1/Parallel/Serial GC changes