Overview of New Features in Java 18

Introduction Java 18 was released on 22nd March 2022, the first release after the Java 17Click to read: Java 17 long-term support version. With nine JDK Enhancement Proposals (JEPs), it was a modest release, but several of its changes affect everyday Java development: UTF-8 became the default character set everywhere, the JDK gained a simple web server, Javadoc got proper code snippets, and finalization was deprecated for removal. This article gives an overview of the new features in Java 18, with links to detailed tutorials. Features for developers
  1. UTF-8 by Default - JEP 400 - APIs which use the default charset now use UTF-8 on every operating system, instead of the platform's encoding, such as Windows-1252 on Windows. Read about UTF-8 by defaultClick to read tutorial: Java 18 UTF-8 by Default: What Changes and Why.
  2. Simple Web Server - JEP 408 - the jwebserver command serves static files from a directory, for prototyping and testing, and SimpleFileServer does the same from Java code. Read the Simple Web Server tutorialClick to read tutorial: Java 18 Simple Web Server (jwebserver) Tutorial.
  3. Code Snippets in Java API Documentation - JEP 413 - the @snippet Javadoc tag for code examples, which can be taken from real, compilable source files. Read about code snippets in JavadocClick to read tutorial: Java 18 Code Snippets in Javadoc with @snippet.
  4. Deprecate Finalization for Removal - JEP 421 - finalize() and the finalization mechanism will be removed; the new option --finalization=disabled shows how an application behaves without it. Read about replacing finalization with CleanerClick to read tutorial: Java 18 Deprecates Finalization: Use Cleaner Instead.
Platform internals
  1. Reimplement Core Reflection with Method Handles - JEP 416 - java.lang.reflect.Method, Constructor and Field were reimplemented on top of method handles, replacing the old bytecode-generating implementation. Application code does not change, but the JDK has less code to maintain, and the change paved the way for later features.
  2. Internet-Address Resolution SPI - JEP 418 - a service-provider interface lets applications plug in their own host name resolver for InetAddress, instead of the operating system's - useful for testing, and for frameworks and virtual threads, which need resolution that does not block an operating system thread.
Previews and incubators
  1. Pattern Matching for switch (Second Preview) - JEP 420 - finalized in Java 21. Read about pattern matching for switchClick to read tutorial: Java 21 Pattern Matching for switch with Examples.
  2. Foreign Function & Memory API (Second Incubator) - JEP 419 - finalized in Java 22. Read the Foreign Function & Memory API tutorialClick to read: Foreign Function & Memory API tutorial.
  3. Vector API (Third Incubator) - JEP 417 - continued incubation of the SIMD Vector API.
Summary In this article we went through the new features of Java 18. UTF-8 became the default charset on every platform, jwebserver made serving files a one-command task, @snippet brought compiler-checked code examples to Javadoc, and finalization was deprecated for removal. Under the hood, core reflection was reimplemented with method handles and host name resolution became pluggable, while pattern matching for switch and the Foreign Function & Memory API continued as previews. Continue with the UTF-8 by default tutorialClick to read tutorial: Java 18 UTF-8 by Default: What Changes and Why, or move on to the new features in Java 19Click to read tutorial: Overview of New Features in Java 19.