Skip to main content
Redhat Developers  Logo
  • Products

    Platforms

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat AI
      Red Hat AI
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • View All Red Hat Products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat Developer Hub
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat OpenShift Local
    • Red Hat Developer Sandbox

      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Secure Development & Architectures

      • Security
      • Secure coding
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • Product Documentation
    • API Catalog
    • Legacy Documentation
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

What's new for developers in Java 18

January 27, 2022
Syed M Shaaf
Related topics:
JavaMicroservices
Related products:
Red Hat OpenShiftRed Hat Enterprise Linux

Share:

    In exciting news for Java developers, Java 18 forked off from the main line at the end of last year and has entered Rampdown Phase Two. This article highlights some of the features that developers can look for in the upcoming Java 18 release, including the new simple web server module, a more sophisticated way to annotate your Javadocs, and the –finalization=disabled option, which lets you test how a Java application will behave when finalization is removed in a future release. See the end of the article for where to download Java 18 in early access builds.

    Java's new simple web server

    Java 18 will provide a minimally functional web server in the jdk.httpserver module. It has an API for access, as well as a binary in the bin directory in case you want to run the server from the command line.

    The command to run the web server can be as simple as:

    $ jwebserver -b 0.0.0.0 -p 8000

    Note: For more command-line options and details about the jdk.httpserver module, see the JEP 408 documentation.

    If you are wondering whether you can implement a full-blown production web server using the simple web server APIs, the answer is no. The web server is definitely not intended for that use. For one thing, it communicates over HTTP/1.1 and doesn't support PUT requests, so it doesn't support dynamic content. The web server is recommended for prototyping, testing, and debugging. An example code snippet with comments follows:

    import java.net.InetSocketAddress;
    import java.nio.file.Path;
    import com.sun.net.httpserver.SimpleFileServer;
    import static com.sun.net.httpserver.SimpleFileServer.OutputLevel;
    
    public class App {
    public static void main( String[] args ){
        // Create a simple file server, given the socket address, path and output level
        var server = SimpleFileServer.createFileServer(new InetSocketAddress(8000),             Path.of("/home/java"), OutputLevel.VERBOSE);
    
        // Starting the server
        server.start();
    
        // A friendly message to get started.
        System.out.println( "We are getting started.. Hello World!" );
      }
    }

    Code snippets in Java API documentation

    Prior to Java 18, developers mostly inserted code samples in Javadoc comments using the @code annotation. That technique is somewhat limited and requires workarounds. For instance, it has always been difficult to validate what's inside the code fragment, implement syntax highlighting, and insert links or escape characters.

    As a new approach, JEP 413 proposes using the @snippet tag. As noted in the JEP, the tag "can be used to declare both inline snippets, where the code fragment is included within the tag itself, and external snippets, where the code fragment is read from a separate source file."

    Here is an example of an inline snippet:

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

    The code lies between curly braces, where the opening brace is followed by the @snippet tag. The Javadoc utility now also includes options to specify links, highlight code, and more. See JEP 413 for more details.

    UTF-8 character set by default

    In prior releases, the default character set was determined when the Java runtime started, and depended on the user's locale and default encoding. Thus, on Windows the charset was windows-1252, whereas on macOS it was UTF-8 except in the POSIX C locale. With Java 18, UTF-8 will be the default for all operating systems.

    You can change the default charset by setting the file.encoding to COMPAT; for instance, by running java -Dfile.encoding=COMPAT. This setting reverts to the algorithm in Java 17.

    Prepare now for the removal of finalize()

    If you have developed Java applications, you are probably familiar with the finalize() method. Since Java 9, the recommendation has been to not use finalize(), but instead to use a try-with-resources statement or the new Cleaner APIs. JEP 421 helps developers prepare for the eventual removal of finalization. You can run an application with the –finalization=disabled option to see how it will behave without the finalize() method. It's great to see this evolution. As a developer, it is also an opportunity to take a closer look at your application's behavior.

    Note: A recent Inside Java podcast episode discusses issues with finalization and what to expect in Java 18. See the documentation for JEP 421 for more details.

    Preview features in Java 18

    Some of the features that were included in the development phase of Java 18 are still in preview. These include the Vector API (JEP 417), which Gunnar Morling discusses in this DevNation Tech Talk. The foreign function and memory API (JEP 419) and pattern matching for switch (JEP 420) also remain in preview.

    Where to download Java 18

    To give Java 18 a try, you can download the early access Eclipse Temurin builds from Eclipse Adoptium. Temurin is the name of the OpenJDK distribution from Adoptium.

    If you are using a current version of Java and want to compare the features with Java 18, I would also highly recommend the Java Almanac. It contains a page listing all the new features and changes in Java 18.

    Conclusion

    This article was a quick look at some of the highlights of Java 18. Keep an eye on the Red Hat Developer OpenJDK page for more Java 18 updates. You can also get started using the Red Hat build of OpenJDK.

    Last updated: January 12, 2024

    Related Posts

    • Explore Java 17 language features with Quarkus

    • Building Java 11 and Gradle containers for OpenShift

    • Introducing the Red Hat build of the OpenJDK Universal Base Images—now in Red Hat Enterprise Linux 8.2

    Recent Posts

    • A deep dive into Apache Kafka's KRaft protocol

    • Staying ahead of artificial intelligence threats

    • Strengthen privacy and security with encrypted DNS in RHEL

    • How to enable Ansible Lightspeed intelligent assistant

    • Why some agentic AI developers are moving code from Python to Rust

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2025 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue