Java 18 : what’s new ?

Java 18 : what’s new ?

Now that Java 18 is features complete (Rampdown Phase One at the day of writing), it’s time to walk throught all the functionalities that brings to us, developers, this new version.

This article is part of a series on what’s new on the last versions of Java, for those who wants to read the others, here are the links : Java 17, Java 16, Java 15, Java 14, Java 13, Java 12, Java 11Java 10, and Java 9.

JEP 400: UTF-8 by Default

With the JEP 400, the default charset becomes UTF-8 for all OS and in all locales.
It is possible to use the file.encoding system property to configure a different default charset or to configure it to COMPAT to revert to the previous mode.

More information in the JEP-400.

JEP 413: Code Snippets in Java API Documentation

Addition of a new JavaDoc @snippet tag which can be used to define a code snippet. This is more flexible than the existing @code tag, does not need to escape special characters, and even allows you to include snippets of code from an external file.

Example of a fragment defined within the JavaDoc:

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

Example of a fragment defined outside the JavaDoc:

/**
 * The following code shows how to use {@code Optional.isPresent}:
 * {@snippet file="ShowOptional.java" region="example"}
 */

This fragment points to the example section of the ShowOptional.java file:

public class ShowOptional {
    void show(Optional<String> v) {
        // @start region="example"
        if (v.isPresent()) {
            System.out.println("v: " + v.get());
        }
        // @end
    }
}

There are several possible options for formatting and including external fragments; your fragments then become executable, you can even define them in JUnit tests for example, so that they are tested with the rest of your code.
More example in this article by Gunnar Morling: Executable JavaDoc Code Snippets .

More information in the JEP-413.

JEP 408: Simple Web Server

This JEP adds to the OpenJDK distribution a minimalist web server that allows you to serve static files from a directory via a command line tool: jwebserver.

By default, this web server binds to localhost (configurable via -b), uses port 80 (configurable via -p), and serves static files in the current directory (configurable via -d).
Each access will be logged in the standard output.

In addition to a command line tool, you can use the SimpleFileServer class to instantiate a web server through its Java API.

var server = SimpleFileServer.createFileServer(new InetSocketAddress(8080), 
        Path.of("/some/path"), OutputLevel.VERBOSE);
server.start()

More examples of the Java API usage in the article Working with the Simple Web Server by Julia Boes.

More information in the JEP-408.

JEP 421: Deprecate Finalization for Removal

Finalization is a mechanism by which it is possible to automatically perform actions just before the destruction of an object. This mechanism has a set of problems, among others, it delays the moment when these actions are performed to a garbage collection and a badly coded finalize() method can resurect the reference to be destroyed.

Since java 9 and the deprecation of the finalize() method of the Object class, it is not recommended to use it anymore and, to free resources linked to a class, it is advisable to use other mechanisms such as the try-with-resource or the new Cleaner api.

JEP 421 takes the removal of finalization a step further by depreciating for removal the finalization mechanism itself. You can now test your code without finalization via the JVM option --finalization=disabled.

More information in the JEP-421.

Internal changes, performance, and security

Each new version of the JDK brings its performance optimizations (including GC and intrisics methods), and security improvements. This one also, and we can note, among other things, optimizations in G1 GC (improvement of the management of remembered sets, see JDK-8017163) and Parallel GC, as well as in the management of encoding of strings.
Another notable addition is support for String de-duplication for the garbage collectors Serial GC, Parallel GC and ZGC.

We can also note two important JEPs on internal mechanisms of the JVM:

  • JEP 418 : Internet-Address Resolution SPI: possibility to change the default implementation of internet address resolution.
  • JEP 416 : Reimplement Core Reflection with Method Handles: previously there have been three implementations to make dynamic method calls: VM native method (internal), dynamic bytecode and Unsafe (used by java.lang.reflect), and finally method handles (java.lang.invoke). The reflection API (java.lang.reflect) has been rewritten to use the MethodHandle API to remove one of them.

Other changes in terms of security have been implemented, you can read this article by Sean Mullan on the subject: JDK 18 Security Enhancements.

Features that remain in preview

The following features remain in preview (or in incubator module).

For details on these, you can refer to my previous articles.

  • JEP-417 – Vector API: third incubation of the functionality.
  • JEP-419 – Foreign Function & Memory API: second incubator for these two features combined in a single JEP.
  • JEP-420 – Pattern Matching for switch: second preview with some improvements.

Miscellaneous

Various JDK additions:

  • Duration.isPositive()
  • Math and StrictMath saw the addition of many new methods : ceilDiv, ceilDivExact, ceilMod, divideExact, floorDivExact and unsignedMultiplyHigh

All the new APIs for JDK 18 can be found in The Java Version Almanac – New APIs in Java 18.

Conclusion

This new version of Java does not bring many new features, but advances the Panama project (Vector API, Foreign Function & Memory API). We hope for Java 19 to have the first JEPs of the Loom (lightweight thread) and Valhalla (value / inline / primitive classes) projects whose first JEPs are being written.

It should still be noted the depreciation of the finalization for deletion, even if its deletion will not be soon, it is still a strong sign in this direction.

2 thoughts on “Java 18 : what’s new ?

  1. Bonjour.

    Je viens d’installer java 18 sur mon PC portable.
    Auparavant, je développais avec java 17.

    Avec java 18, la compilation ne fonctionne plus.
    Pour cause, les premières lignes de mon programme comportent des caractères accentués ‘é, …’
    Je bloque à ce niveau.
    Merci de votre aide à la résolution du problème, car je ne comprends pas ce changement avec UTF-8 !!

    1. Difficile de t’aider avec si peu d’information.
      Je te conseille de te tourner vers StackOverflow ou Developpez.com où tu pourras poser une question (avec code et message d’erreur) et trouver de l’aide.

Leave a Reply to ALVAREZ PedroCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.