What are the new features of java-18?

On March 2022, Oracle released a new version of Java as Java-18. Following are the new features of java-18.

UTF-8 by Default

  • Previously default charset of Java was determined by the host operating system and locale.  
  • In java-18 onwards default charset of the platform is UTF-8 since this charset is most widely used in the world.

Simple Web Server

  • This provides a command line tool to start a minimal web server that servers static files only. 
  • For this no CGI or servlet-like functionality is available.
  • This tool will be useful for prototyping, ad-hoc coding, and testing purposes, particularly in educational contexts.

Code Snippets in Java API Documentation

  • Java-18 release introduced a new inline tag {@snippet …}, to declare code fragments to appear in the generated documentation.
  • It 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.
  • Additional details about the snippet can be given as attributes, in the form of name=value pairs, placed after the initial tag name

Reimplement Core Reflection with Method Handles

  • Reimplement java.lang.reflect on top of method handles as the common underlying reflective mechanism of the platform by replacing the bytecode-generating implementations of Method::invoke, Constructor::newInstance, Field::get, and Field::set.
  • The new implementation performs direct invocations of the method handles for specific reflective objects

Vector API (Third Incubator)

  • The Vector API was first integrated into Java-16 as an incubating API. A second round of incubation was integrated into Java-17.
  • Java-18 incorporates enhancements in response to feedback as well as performance improvements and other significant implementation enhancements are added. 
  • Java-18 includes the following notable changes:
    • Support the ARM Scalar Vector Extension (SVE) platform.
    • Improve the performance of vector operations that accept masks on architectures that support masking in hardware.

Internet-Address Resolution SPI

  • Define a service-provider interface (SPI) for hostname and address resolution, so that java.net.InetAddress can make use of resolvers other than the platform’s built-in resolver.
  • The java.net.InetAddress API resolves host names to Internet Protocol (IP) addresses, and vice versa. 
  • The API currently uses the operating system’s native resolver, which is typically configured to use a combination of a local host file and the Domain Name System (DNS).

Foreign Function & Memory API (Second Incubator)

  • Java-18 introduced an API by which Java programs can interoperate with code and data outside of the Java runtime. By efficiently invoking foreign functions (i.e., code outside the JVM), and by safely accessing foreign memory (i.e., memory not managed by the JVM), the API enables Java programs to call native libraries and process native data without the brittleness and danger of JNI.
  • The Foreign Function & Memory API was first introduced in Java 17 as an incubating API.
  • The following changes are included as a part of Java-18:
    • Support for more carriers, such as boolean and MemoryAddress, in memory access var handles;
    • A more general dereference API is available in both the MemorySegment and MemoryAddress interfaces;
    • A simpler API to obtain downcall method handles, where passing a MethodType parameter is no longer required;
    • A simpler API to manage temporal dependencies between resource scopes; and
    • A new API to copy Java arrays to and from memory segments.

Pattern Matching for the switch (Second Preview)

  • Pattern Matching for the switch was introduced in JDK 17 as a preview feature. Java-18 proposes a second preview of the feature with minor refinements.
  • The enhancements since the first preview are:
    • Dominance checking now forces a constant case label to appear before a guarded pattern of the same type, for readability.
    • Exhaustiveness checking of switch blocks is now more precise with sealed hierarchies where the permitted direct subclass only extends an instantiation of the (generic) sealed superclass.

Deprecate Finalization for Removal

  • Finalization, introduced in Java 1.0, was intended to help avoid resource leaks. 
  • A class can declare a finalizer — the method protected void finalize() — whose body releases any underlying resources. 
  • The GC will schedule the finalizer of an unreachable object to be called before it reclaims the object’s memory; in turn, the finalize method can take actions such as calling the object’s close method.
  • Finalization has several critical, fundamental flaws
    • Unpredictable latency: An arbitrarily long time may pass between the moment an object becomes unreachable and the moment its finalizer is called. In fact, the GC provides no guarantee that any finalizer will ever be called.
    • Unconstrained behavior: Finalizer code can take any action. In particular, it can save a reference to the object being finalized, thereby resurrecting the object and making it reachable once again.
    • Always enabled: Finalization has no explicit registration mechanism. A class with a finalizer enables finalization for every instance of the class, whether needed or not. The finalization of an object cannot be canceled, even if it is no longer necessary for that object.
    • Unspecified threading: Finalizers run on unspecified threads, in an arbitrary order. Neither threading nor ordering can be controlled.
  • Alternate option:
    • Developers should use alternative techniques to avoid resource leaks, namely try-with-resources, and cleaners.

Also, see:

-A blog by Shwetali Khambe

Related Posts