Martin Ahrer

Thinking outside the box

Java 18 major language and API improvements

2024-08-29 3 min read Martin

Java 18 has been released in March 2022.

The following content is summarizing the most important changes to the Java API, the Java language, and the JVM introduced with Java 18.

For a more complete overview follow the links in the following sections referring to the official Oracle release documents.

Java 18 Language improvements

The following describes selected improvements of the Java language. See the Java 18 release notes. See Java Language Updates for Java SE 18.

Java 18 is introducing the following language enhancements as developer preview.

  • JEP 420 is continuing with Pattern Matching for switch.

We are not diving into the details of these preview features and defer that until they are moving from preview for general availability.

Java 18 JVM improvements

The following describes selected improvements of the Java JVM. See the Java 18 release notes.

With JEP-400, now UTF-8 is the default charset of the standard Java APIs.

In JDK 17 and earlier, the default charset is determined when the Java runtime starts. On macOS, it is UTF-8 except in the POSIX C locale. On other operating systems, it depends upon the user’s locale and the default encoding, e.g., on Windows, it is a codepage-based charset such as windows-1252 or windows-31j.

Java 18 Tooling improvements

The following describes selected improvements of the Java tooling. See the Java 18 release notes.

Java 18 is introducing the following tooling enhancements.

  • JEP 408 is adding the jwebserver tool that can serve static files and support the HEAD and GET methods. This is simplifying situations where a simple web server is needed for example for testing and eliminating the need for external dependencies. In case a more customized web server is required, we can use the implementations from the package com.sun.net.httpserver which constitutes the base for jwebserver.

  • JEP 413 is adding support to include Java code snippets either from external sources of inline snippets. Nicolai Parlog has a pretty decent summary of some of the features builtin. See https://nipafx.dev/inside-java-newscast-20/#code-in-javadoc

Java Doc Snippet Tag

With a snippet region defined we can include a fragment of compileable and tested code.

Snippet Include
  /**
   * The following code is included from the defined region
   * {@snippet class="eighteen.tooling.JavaDocSnippetTag" region="snippetInclude"
   * }
   */
  @Paragraph("""
        With a snippet region defined we can include a fragment of compileable and tested code.
        """)
// @start region="snippetInclude"
  @Test
  public void snippetInclude() {

  }
// @end region="snippetInclude"

With a snippet tag we can just inline code. There is no need to escape or use HTML.

Snippet Inline
/**
 * The following code is inlined
 *
 * {@snippet :
 * public void inlined() {
 * }
 * }
 */