Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

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

    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

    • Protect data offloaded to GPU-accelerated environments with OpenShift sandboxed containers

    • Case study: Measuring energy efficiency on the x64 platform

    • How to prevent AI inference stack silent failures

    • Preventing GPU waste: A guide to JIT checkpointing with Kubeflow Trainer on OpenShift AI

    • How to manage TLS certificates used by OpenShift GitOps operator

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    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
    © 2026 Red Hat

    Red Hat legal and privacy links

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

    Chat Support

    Please log in with your Red Hat account to access chat support.