Java 18

created onOctober 6, 2022

general availability on 2022-03-22

JEPs

new features

JEP state summary /remark
JEP 400: UTF-8 by Default standard specifies UTF-8 as the default charset of the standard Java APIs. APIs that depend upon the default charset will behave consistently across all implementations, operating systems, locales, and configurations.
JEP 408: Simple Web Server standard provides a command-line tool to start a minimal web server that serves static files only (no CGI or servlet-like functionality) for prototyping, ad-hoc coding, and testing purposes, particularly in educational contexts.
JEP 413: Code Snippets in Java API Documentation standard introduces an @snippet tag for JavaDoc’s Standard Doclet, to simplify the inclusion of example source code in API documentation.
JEP 417: Vector API third incubator provide a third iteration of an incubator module, jdk.incubator.vector, to express vector computations that reliably compile at runtime to optimal vector hardware instructions on supported CPU architectures and thus achieve superior performance to equivalent scalar computations.
JEP 418: Internet-Address Resolution SPI standard define a service-provider interface (SPI) for host name and address resolution, so that java.net.InetAddress can make use of resolvers other than the platform’s built-in resolver.
JEP 419: Foreign Function & Memory API second incubator eases the acces for devs to code and data on the same machine as the JVM but outside of the JVM, without the drawbacks of JNI.
JEP 420: Pattern Matching for switch second preview introduces pattern matching for switch expressions and statements, along with extensions to the language of patterns. See comments on JEP 406: Pattern Matching for switch (Preview).

JDK internal

JEP summary /remark
JEP 416: Reimplement Core Reflection with Method Handles reimplement java.lang.reflect.Method, Constructor, and Field on top of java.lang.invoke method handles. Making method handles the underlying mechanism for reflection will reduce the maintenance and development cost of both the java.lang.reflect and java.lang.invoke APIs.

deprecated

JEP summary /remark
JEP 421: Deprecate Finalization for Removal deprecate finalization for removal in a future release. Finalization remains enabled by default for now, but can be disabled to facilitate early testing. In a future release it will be disabled by default, and in a later release it will be removed. Note that finalization is distinct from both the finally block of the try-finally construct. No changes are proposed to try-finally.

removed

none

some feature details

JEP 413: Code Snippets in Java API Documentation

Short fragments of code (oneliners) are usually included into javadoc with @code: like in the example below:

{@code System.out.println( "hi there" );}

Longer code fragments can be wrapped into the HTML tag:

<pre>{@code ... some lines of code }</pre>

inline snippets

An inline snippet contains the content of the snippet within the tag, i.e.:

/** The following code shows how to use {@code Optional.isPresent}: {@snippet : static void check(Object o) { switch(Object o){ case String s, Integer i->{ // some code } } } } */

The content of the snippet, which is included in the generated documentation, is the text between the newline after the colon ‘:’ and the closing curly brace ‘}’.

external snippets

An external snippet refers to a separate file that contains the content of the snippet. In an external snippet the colon, newline, and subsequent content can be omitted.

/** the following code shows how to use pattern matching for switch: {@snippet file="PatternMatching.java" region="example"} */

with PatternMatching.java:

// @start region="example" static void check(Object o) { switch(Object o){ case String s -> { ... } case Integer i -> { ... } } } // @end

Here the code between and is included into the javadoc. If those two markers are omitted, the whole file is included.

Personally, I’m not yet shure if I like the idea of source code being cluttered with markers for documentation.

other features of code snippets

There is a third kind of code snippets, the hybrid snippet, which is a combination of an inline snippet and an external snippet. Other features include text highlighting and linking of text to declarations elsewhere in the API.

Additionally to Java source code files Code Snippets support a bunch of other files formats, but the JEP is unclear about what formats are supported. Apart from Java source code, property files are mentioned.

reference

OpenJDK JDK 18 Feature List and Schedule

JEP 413: Code Snippets in Java API Documentation