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
Platform internals
Summary
In this article we went through the new features of Java 18. UTF-8 became the default charset on every platform,
- 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.
- Simple Web Server - JEP 408 - the
jwebservercommand serves static files from a directory, for prototyping and testing, andSimpleFileServerdoes the same from Java code. Read the Simple Web Server tutorialClick to read tutorial: Java 18 Simple Web Server (jwebserver) Tutorial. - Code Snippets in Java API Documentation - JEP 413 - the
@snippetJavadoc 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. - Deprecate Finalization for Removal - JEP 421 -
finalize()and the finalization mechanism will be removed; the new option--finalization=disabledshows how an application behaves without it. Read about replacing finalization with CleanerClick to read tutorial: Java 18 Deprecates Finalization: Use Cleaner Instead.
- Reimplement Core Reflection with Method Handles - JEP 416 -
java.lang.reflect.Method,ConstructorandFieldwere 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. - 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.
- 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.
- 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.
- Vector API (Third Incubator) - JEP 417 - continued incubation of the SIMD Vector API.
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.Continue Reading Java 18 to 21 Tutorials
Java 18 UTF-8 by Default: What Changes and WhyClick to read tutorial: Java 18 UTF-8 by Default: What Changes and Why Java 18 Simple Web Server (jwebserver) TutorialClick to read tutorial: Java 18 Simple Web Server (jwebserver) Tutorial Java 18 Code Snippets in Javadoc with @snippetClick to read tutorial: Java 18 Code Snippets in Javadoc with @snippet Java 18 Deprecates Finalization: Use Cleaner InsteadClick to read tutorial: Java 18 Deprecates Finalization: Use Cleaner Instead Overview of New Features in Java 19Click to read tutorial: Overview of New Features in Java 19 Overview of New Features in Java 20Click to read tutorial: Overview of New Features in Java 20 Overview of New Features in Java 21Click to read tutorial: Overview of New Features in Java 21 Java 21 Virtual Threads Tutorial with ExamplesClick to read tutorial: Java 21 Virtual Threads Tutorial with Examples Java 21 Record Patterns with ExamplesClick to read tutorial: Java 21 Record Patterns with Examples
Java 18 UTF-8 by Default: What Changes and WhyClick to read tutorial: Java 18 UTF-8 by Default: What Changes and Why Java 18 Simple Web Server (jwebserver) TutorialClick to read tutorial: Java 18 Simple Web Server (jwebserver) Tutorial Java 18 Code Snippets in Javadoc with @snippetClick to read tutorial: Java 18 Code Snippets in Javadoc with @snippet Java 18 Deprecates Finalization: Use Cleaner InsteadClick to read tutorial: Java 18 Deprecates Finalization: Use Cleaner Instead Overview of New Features in Java 19Click to read tutorial: Overview of New Features in Java 19 Overview of New Features in Java 20Click to read tutorial: Overview of New Features in Java 20 Overview of New Features in Java 21Click to read tutorial: Overview of New Features in Java 21 Java 21 Virtual Threads Tutorial with ExamplesClick to read tutorial: Java 21 Virtual Threads Tutorial with Examples Java 21 Record Patterns with ExamplesClick to read tutorial: Java 21 Record Patterns with Examples