{"id":1375,"date":"2022-01-04T12:46:41","date_gmt":"2022-01-04T11:46:41","guid":{"rendered":"https:\/\/www.loicmathieu.fr\/wordpress\/?p=1375"},"modified":"2022-03-25T12:33:01","modified_gmt":"2022-03-25T11:33:01","slug":"java-18-quoi-de-neuf","status":"publish","type":"post","link":"https:\/\/www.loicmathieu.fr\/wordpress\/informatique\/java-18-quoi-de-neuf\/","title":{"rendered":"Java 18 : what&#8217;s new ?"},"content":{"rendered":"<p>Now that Java 18 is features complete (Rampdown Phase One at the day of writing), it\u2019s time to walk throught all the functionalities that brings to us, developers, this new version.<\/p>\n<p>This article is part of a series on <a href=\"https:\/\/www.loicmathieu.fr\/wordpress\/tag\/whatsnew\/\">what\u2019s new on the last versions of Java<\/a>, for those who wants to read the others, here are the links : <a href=\"https:\/\/www.loicmathieu.fr\/wordpress\/informatique\/java-17-quoi-de-neuf\/\">Java 17<\/a>, <a href=\"https:\/\/www.loicmathieu.fr\/wordpress\/informatique\/java-16-quoi-de-neuf\/\">Java 16<\/a>, <a href=\"https:\/\/www.loicmathieu.fr\/wordpress\/informatique\/java-15-quoi-de-neuf\/\">Java 15<\/a>, <a href=\"http:\/\/www.loicmathieu.fr\/wordpress\/informatique\/java-14-quoi-de-neuf\/\">Java 14<\/a>, <a href=\"http:\/\/www.loicmathieu.fr\/wordpress\/informatique\/java-13-quoi-de-neuf\/\">Java 13<\/a>, <a href=\"http:\/\/www.loicmathieu.fr\/wordpress\/informatique\/java-12-quoi-de-neuf\/\">Java 12<\/a>, <a href=\"http:\/\/www.loicmathieu.fr\/wordpress\/informatique\/java-11-quoi-de-neuf\/\">Java 11<\/a>,\u00a0<a href=\"http:\/\/www.loicmathieu.fr\/wordpress\/informatique\/java-10-quoi-de-neuf\/\">Java 10,<\/a>\u00a0and\u00a0<a href=\"http:\/\/www.loicmathieu.fr\/wordpress\/informatique\/les-nouveautes-de-java-9-pour-les-developeurs\/\">Java 9<\/a>.<\/p>\n<h2>JEP 400: UTF-8 by Default<\/h2>\n<p>With the JEP 400, the default charset becomes UTF-8 for all OS and in all locales.\nIt is possible to use the <code>file.encoding<\/code> system property to configure a different default charset or to configure it to <code>COMPAT<\/code> to revert to the previous mode.<\/p>\n<p>More information in the <a href=\"https:\/\/openjdk.java.net\/jeps\/400\" rel=\"noopener\" target=\"_blank\">JEP-400<\/a>.<\/p>\n<h2>JEP 413: Code Snippets in Java API Documentation<\/h2>\n<p>Addition of a new JavaDoc <a>@snippet<\/code>code&gt;@snippet&lt;\/code<\/a> tag which can be used to define a code snippet. This is more flexible than the existing <a>@code<\/code>code&gt;@code&lt;\/code<\/a> tag, does not need to escape special characters, and even allows you to include snippets of code from an external file.<\/p>\n<p>Example of a fragment defined within the JavaDoc:<\/p>\n<pre>\n\/**\n * The following code shows how to use {@code Optional.isPresent}:\n * {@snippet :\n * if (v.isPresent()) {\n *     System.out.println(\"v: \" + v.get());\n * }\n * }\n *\/\n<\/pre>\n<p>Example of a fragment defined outside the JavaDoc:<\/p>\n<pre>\n\/**\n * The following code shows how to use {@code Optional.isPresent}:\n * {@snippet file=\"ShowOptional.java\" region=\"example\"}\n *\/\n<\/pre>\n<p>This fragment points to the <strong>example<\/strong> section of the <code>ShowOptional.java<\/code> file:<\/p>\n<pre>\npublic class ShowOptional {\n    void show(Optional v) {\n        \/\/ @start region=\"example\"\n        if (v.isPresent()) {\n            System.out.println(\"v: \" + v.get());\n        }\n        \/\/ @end\n    }\n}\n<\/pre>\n<p>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.\nMore example in this article by Gunnar Morling: <a href=\"https:\/\/www.morling.dev\/blog\/executable-javadoc-code-snippets\/\" rel=\"noopener\" target=\"_blank\"> Executable JavaDoc Code Snippets <\/a>.<\/p>\n<p>More information in the <a href=\"https:\/\/openjdk.java.net\/jeps\/413\" rel=\"noopener\" target=\"_blank\">JEP-413<\/a>.<\/p>\n<h2>JEP 408: Simple Web Server<\/h2>\n<p>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: <code>jwebserver<\/code>.<\/p>\n<p>By default, this web server binds to localhost (configurable via <code>-b<\/code>), uses port 80 (configurable via <code>-p<\/code>), and serves static files in the current directory (configurable via <code>-d<\/code>).\nEach access will be logged in the standard output.<\/p>\n<p>In addition to a command line tool, you can use the <code>SimpleFileServer<\/code> class to instantiate a web server through its Java API.<\/p>\n<pre>\nvar server = SimpleFileServer.createFileServer(new InetSocketAddress(8080), \n        Path.of(\"\/some\/path\"), OutputLevel.VERBOSE);\nserver.start()\n<\/pre>\n<p>More examples of the Java API usage in the article <a href=\"https:\/\/inside.java\/2021\/12\/06\/working-with-the-simple-web-server\/\" rel=\"noopener\" target=\"_ blank\">Working with the Simple Web Server<\/a> by Julia Boes.<\/p>\n<p>More information in the <a href=\"https:\/\/openjdk.java.net\/jeps\/408\" rel=\"noopener\" target=\"_blank\">JEP-408<\/a>.<\/p>\n<h2>JEP 421: Deprecate Finalization for Removal<\/h2>\n<p>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 <code>finalize()<\/code> method can resurect the reference to be destroyed.<\/p>\n<p>Since java 9 and the deprecation of the <code>finalize()<\/code> method of the <code> Object <\/code> 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 <code>try-with-resource<\/code> or the new <code>Cleaner<\/code> api.<\/p>\n<p>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 <code>--finalization=disabled<\/code>.<\/p>\n<p>More information in the <a href=\"https:\/\/openjdk.java.net\/jeps\/421\" rel=\"noopener\" target=\"_blank\">JEP-421<\/a>.<\/p>\n<h2>Internal changes, performance, and security<\/h2>\n<p>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 <a>JDK-8017163<\/a>) and Parallel GC, as well as in the management of encoding of strings.\nAnother notable addition is support for <a href=\"http:\/\/openjdk.java.net\/jeps\/192\" rel=\"noopener\" target=\"_blank\">String de-duplication<\/a> for the garbage collectors Serial GC, Parallel GC and ZGC.<\/p>\n<p>We can also note two important JEPs on internal mechanisms of the JVM:<\/p>\n<ul><li> <a href=\"https:\/\/openjdk.java.net\/jeps\/418\" rel=\"noopener\" target=\"_blank\"> JEP 418 <\/a>: Internet-Address Resolution SPI: possibility to change the default implementation of internet address resolution.<\/li>\n<li> <a href=\"https:\/\/openjdk.java.net\/jeps\/416\" rel=\"noopener\" target=\"_blank\"> JEP 416 <\/a>: 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. <\/li>\n<\/ul>\n<p>Other changes in terms of security have been implemented, you can read this article by Sean Mullan on the subject: <a href=\"https:\/\/seanjmullan.org\/blog\/2022\/03\/23\/jdk18\" rel=\"noopener\" target=\"_blank\">JDK 18 Security Enhancements<\/a>.<\/p>\n<h2>Features that remain in preview<\/h2>\n<p>The following features remain in preview (or in incubator module).<\/p>\n<p>For details on these, you can refer to my previous articles.<\/p>\n<ul><li> <a href=\"https:\/\/openjdk.java.net\/jeps\/417\" rel=\"noopener\" target=\"_blank\"> JEP-417 <\/a> &#8211; Vector API: third incubation of the functionality. <\/li>\n<li> <a href=\"https:\/\/openjdk.java.net\/jeps\/419\" rel=\"noopener\" target=\"_blank\"> JEP-419 <\/a> &#8211; Foreign Function &amp; Memory API: second incubator for these two features combined in a single JEP. <\/li>\n<li> <a href=\"https:\/\/openjdk.java.net\/jeps\/420\" rel=\"noopener\" target=\"_blank\"> JEP-420 <\/a> &#8211; Pattern Matching for switch: second preview with some improvements. <\/li>\n<\/ul>\n<h2>Miscellaneous<\/h2>\n<p>Various JDK additions:<\/p>\n<ul><li><code>Duration.isPositive()<\/code><\/li>\n\n<li><code>Math<\/code> and <code>StrictMath<\/code> saw the addition of many new methods : <code>ceilDiv<\/code>, <code>ceilDivExact<\/code>, <code>ceilMod<\/code>, <code>divideExact<\/code>, <code>floorDivExact<\/code> and <code>unsignedMultiplyHigh<\/code><\/li>\n<\/ul>\n<p>All the new APIs for JDK 18 can be found in <a href=\"https:\/\/javaalmanac.io\/jdk\/18\/apidiff\/17\/\" rel=\"noopener\" target=\"_blank\">The Java Version Almanac &#8211; New APIs in Java 18<\/a>.<\/p>\n<h2>Conclusion<\/h2>\n<p>This new version of Java does not bring many new features, but advances the Panama project (Vector API, Foreign Function &amp; 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.<\/p>\n<p>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.<\/p>","protected":false},"excerpt":{"rendered":"<p>Now that Java 18 is features complete (Rampdown Phase One at the day of writing), it\u2019s time to walk throught all the functionalities that brings to us, developers, this new version. This article is part of a series on what\u2019s 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 11,\u00a0Java 10,\u00a0and\u00a0Java 9. JEP 400: UTF-8 by&#8230;<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/www.loicmathieu.fr\/wordpress\/informatique\/java-18-quoi-de-neuf\/\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p><\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":4,"activitypub_interaction_policy_quote":"anyone","activitypub_status":"","footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[9],"tags":[11,201,163],"class_list":["post-1375","post","type-post","status-publish","format-standard","hentry","category-informatique","tag-java","tag-java18","tag-whatsnew"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":856,"url":"https:\/\/www.loicmathieu.fr\/wordpress\/informatique\/java-next\/","url_meta":{"origin":1375,"position":0},"title":"Java.Next","author":"admin","date":"Wednesday October 31st, 2018","format":false,"excerpt":"Ma premi\u00e8re contribution au blog de Zenika est un article qui parle du futur (ou du pr\u00e9sent) de Java et des changement pour les d\u00e9veloppeurs des version 9, 10 et 11. La gouvernance de Java y est aussi abord\u00e9. Cet article reprend et r\u00e9sume les articles que j'ai pr\u00e9c\u00e9dement \u00e9crit\u2026","rel":"","context":"In &quot;informatique&quot;","block_context":{"text":"informatique","link":"https:\/\/www.loicmathieu.fr\/wordpress\/category\/informatique\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":829,"url":"https:\/\/www.loicmathieu.fr\/wordpress\/informatique\/java-11-quoi-de-neuf\/","url_meta":{"origin":1375,"position":1},"title":"Java 11  : what\u2019s new ?","author":"admin","date":"Monday October  1st, 2018","format":false,"excerpt":"Now that Java 11 is out, it is time to look at the new features that this version brings to us, developers. This article is part of a series on what\u2019s new on the last versions of Java, for those who wants to read the others, here are the links\u2026","rel":"","context":"In &quot;informatique&quot;","block_context":{"text":"informatique","link":"https:\/\/www.loicmathieu.fr\/wordpress\/category\/informatique\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":712,"url":"https:\/\/www.loicmathieu.fr\/wordpress\/informatique\/demarrage-jvm-8-vs-9\/","url_meta":{"origin":1375,"position":2},"title":"D\u00e9marrage JVM 8 vs 9","author":"admin","date":"Thursday August 31st, 2017","format":false,"excerpt":"Introduction En parcourant la mailing liste d'open JDK (core-lib-dev) j'ai vu plusieurs threads de mail \u00e0 propos d'optimisation de temps de d\u00e9marrage et d'occupation m\u00e9moire d'une JVM \"minimale\". Ce travail a \u00e9t\u00e9 r\u00e9alis\u00e9 en grande partie par Claes Redestad (Oracle) lors du d\u00e9veloppement de Java 9. J'ai donc d\u00e9cid\u00e9 de\u2026","rel":"","context":"In &quot;informatique&quot;","block_context":{"text":"informatique","link":"https:\/\/www.loicmathieu.fr\/wordpress\/category\/informatique\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":722,"url":"https:\/\/www.loicmathieu.fr\/wordpress\/informatique\/java-10-quoi-de-neuf\/","url_meta":{"origin":1375,"position":3},"title":"Java 10 : what&#8217;s new ?","author":"admin","date":"Monday March 26th, 2018","format":false,"excerpt":"Now that java 10 is out, it's time to look at all the new functionalities of this version. Like my previous article on Java 9, I will focus on the changes that will impact developers that uses Java leaving aside the changes that are internal\/very small\/on rarely used API. The\u2026","rel":"","context":"In &quot;informatique&quot;","block_context":{"text":"informatique","link":"https:\/\/www.loicmathieu.fr\/wordpress\/category\/informatique\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1839,"url":"https:\/\/www.loicmathieu.fr\/wordpress\/informatique\/java-vers-une-integrite-par-defaut-de-la-jvm\/","url_meta":{"origin":1375,"position":4},"title":"Java: towards JVM integrity by default","author":"admin","date":"Tuesday February  4th, 2025","format":false,"excerpt":"This article first appeared in Programmez! Hors s\u00e9rie #16 (in french only). The Java Virtual Machine (JVM) is an execution environment that enables programs written in Java (or other languages compiled into Java bytecode) to run on different operating systems and hardware architectures. From the begining, the JVM was designed\u2026","rel":"","context":"In &quot;informatique&quot;","block_context":{"text":"informatique","link":"https:\/\/www.loicmathieu.fr\/wordpress\/category\/informatique\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1112,"url":"https:\/\/www.loicmathieu.fr\/wordpress\/informatique\/java-15-quoi-de-neuf\/","url_meta":{"origin":1375,"position":5},"title":"Java 15 : what&#8217;s new ?","author":"admin","date":"Thursday July  2nd, 2020","format":false,"excerpt":"Now that Java 15 is features complete (Rampdown Phase One at the day of writing), it\u2019s time to walk throught all it\u2019s functionalities that brings to us, developers, this new version. This article is part of a series on what\u2019s new on the last versions of Java, for those who\u2026","rel":"","context":"In &quot;informatique&quot;","block_context":{"text":"informatique","link":"https:\/\/www.loicmathieu.fr\/wordpress\/category\/informatique\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.loicmathieu.fr\/wordpress\/wp-json\/wp\/v2\/posts\/1375","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.loicmathieu.fr\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.loicmathieu.fr\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.loicmathieu.fr\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.loicmathieu.fr\/wordpress\/wp-json\/wp\/v2\/comments?post=1375"}],"version-history":[{"count":1,"href":"https:\/\/www.loicmathieu.fr\/wordpress\/wp-json\/wp\/v2\/posts\/1375\/revisions"}],"predecessor-version":[{"id":1449,"href":"https:\/\/www.loicmathieu.fr\/wordpress\/wp-json\/wp\/v2\/posts\/1375\/revisions\/1449"}],"wp:attachment":[{"href":"https:\/\/www.loicmathieu.fr\/wordpress\/wp-json\/wp\/v2\/media?parent=1375"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.loicmathieu.fr\/wordpress\/wp-json\/wp\/v2\/categories?post=1375"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.loicmathieu.fr\/wordpress\/wp-json\/wp\/v2\/tags?post=1375"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}