{"id":1572,"date":"2022-12-14T17:47:06","date_gmt":"2022-12-14T16:47:06","guid":{"rendered":"https:\/\/www.loicmathieu.fr\/wordpress\/?p=1572"},"modified":"2023-07-31T14:04:54","modified_gmt":"2023-07-31T12:04:54","slug":"java-20-quoi-de-neuf","status":"publish","type":"post","link":"https:\/\/www.loicmathieu.fr\/wordpress\/informatique\/java-20-quoi-de-neuf\/","title":{"rendered":"Java 20: what&#8217;s new ?"},"content":{"rendered":"<p>Now that Java 20 is features complete (Rampdown Phase One at the day of writing), it\u2019s time to walk through all the functionalities that bring 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-19-quoi-de-neuf\/\">Java 19<\/a>, <a href=\"https:\/\/www.loicmathieu.fr\/wordpress\/informatique\/java-18-quoi-de-neuf\/\">Java 18<\/a>, <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<p>We have to admit that there is not a lot of new things in this release. Of the 6 Java Enhancement Proposals (JEP) that have been released, only one is about a new language API: JEP 429 &#8211; Scoped Values, the others are iterations of existing features.<\/p>\n<h2>JEP 429 &#8211; Scoped Values<\/h2>\n<p>JEP 429 introduces <strong>Scoped Values<\/strong> which allow sharing of immutable data within and between threads.<\/p>\n<p>Until now, to share data within a thread, Thread Locals were used. These are mutable and require a complex data structure whose cost is not in line with virtual threads which are light and cheap to build. Moreover, they were sometimes the cause of bugs, because their use was not necessarily well understood.<\/p>\n<p>Since the support of thread locals with Loom was problematic, a new way to share data between threads has been proposed which is immutable and interfaces better with the Structured Concurrency API: Scoped Values.<\/p>\n<p>Here is a very basic example:<\/p>\n<pre>\nprivate static final ScopedValue USERNAME = ScopedValue.newInstance();\n\nScopedValue.where(USERNAME, \"duke\") \/\/ bind a value to the scope\n    \/\/ start a thread that could access this value\n    .run(() -&gt; System.out.println(\"User: \" + USERNAME.get())); \n<\/pre>\n<p>It is also possible to call <code>.call()<\/code> with a <code>Callable<\/code> to return a value.<\/p>\n<p>At the end of the <code>run()<\/code> method call, the scoped value will no longer have a value for the thread, which avoids any risk of memory leak.<\/p>\n<p>More information in the <a href=\"https:\/\/openjdk.java.net\/jeps\/429\" rel=\"noopener\" target=\"_blank\">JEP 429<\/a>.<\/p>\n<h2>JEP-432 &#8211; Record Patterns (Second Preview)<\/h2>\n<p>The second preview of Record Patterns contains important changes, so it deserves a dedicated paragraph to present them.<\/p>\n<p>If you don&#8217;t know Record Patterns yet, you can refer to my article <a href=\"https:\/\/www.loicmathieu.fr\/wordpress\/informatique\/java-19-quoi-de-neuf\/\" rel=\"noopener\" target=\"_blank\">Java 19: What&#8217;s new?<\/a><\/p>\n<p>Three changes has been done:<\/p>\n<ul><li>Support for parameterized type inference for generic record patterns.<\/li>\n\n<li>Support for record patterns in for loops (enhanced for statements).<\/li>\n\n<li>Removed support for named record patterns.<\/li>\n<\/ul>\n<p>The support of parameterized type inference for generic record patterns allows to omit the parameterized type in a record pattern if it can be inferred by the compiler. Surprisingly, the diamond () operator has not been used, we can simply omit the parameterized type.<\/p>\n<pre>\nrecord Box(T t) {}\n\nstatic void test(Box bo) {\n    if (bo instanceof Box(var s)) { \/\/ Inferred to be Box(var s)\n        System.out.println(\"String \" + s);\n    }\n}\n<\/pre>\n<p>The support of record patterns in for loops (enhanced for statements) allows direct access to the components of a record inside the body of a for loop.<\/p>\n<pre>\nrecord Point(int x, int y) {}\n\nstatic void dump(Point[] pointArray) {\n    for (Point(var x, var y) : pointArray) { \/\/ Record Pattern in header!\n        System.out.println(\"(\" + x + \", \" + y + \")\");\n    }\n}\n<\/pre>\n<p>Before JEP 432, it was possible to name a record pattern to, in a way, do pattern matching on the record and its components at the same time.<\/p>\n<pre>\nrecord Point(int x, int y) {}\n\nstatic void noMorePossible(Point p) {\n    if(p instanceof Point(int x, int y) p) {\n        System.out.println(\"x=\" + x + \" y=\" + y + \"for the point \" + p);\n    }\n}\n<\/pre>\n<p>This possibility has been removed, the above code will now generate an error at compile time. It is not yet known if this is permanent or if it could be supported later.<\/p>\n<h2>Features that remain in preview<\/h2>\n<p>The following features remain in preview (or in incubator module).<\/p>\n<ul><li><a href=\"https:\/\/openjdk.java.net\/jeps\/433\" rel=\"noopener\" target=\"_blank\">JEP-433<\/a> &#8211; Pattern Matching for switch (Fourth Preview): change in the exception thrown by a switch of an enum in the case where the switch is supposed to be exhaustive (one case for each value of the enum) but is not at runtime. The exception was of type <code>IncompatibleClassChangeError<\/code> and is now of type <code>MatchException<\/code>.<\/li>\n\n<li><a href=\"https:\/\/openjdk.java.net\/jeps\/434\" rel=\"noopener\" target=\"_blank\">JEP-434<\/a> &#8211; Foreign Function &amp; Memory API (Second Preview): minor evolution of the API to make it easier to use.<\/li>\n\n<li><a href=\"https:\/\/openjdk.java.net\/jeps\/436\" rel=\"noopener\" target=\"_blank\">JEP-436<\/a> &#8211; Virtual Threads (Second Preview): no significant change, the feature remains in preview to have more feedback on its usage.<\/li>\n\n<li><a href=\"https:\/\/openjdk.java.net\/jeps\/437\" rel=\"noopener\" target=\"_blank\">JEP-437<\/a> &#8211; Structured Concurrency (Second Incubator): added inheritance of scoped values (JEP 429)<\/li>\n\n<li><a href=\"https:\/\/openjdk.org\/jeps\/438\" title=\"JEP 438\" rel=\"noopener\" target=\"_blank\">JEP 438<\/a> &#8211; Vector API (Fifth Incubator) : small bugfixes and performance improvements.<\/li>\n<\/ul>\n<p>For details on these, you can refer to my previous articles.<\/p>\n<p>More information in the <a href=\"https:\/\/openjdk.java.net\/jeps\/432\" rel=\"noopener\" target=\"_blank\">JEP 432<\/a>.<\/p>\n<h2>Miscellaneous<\/h2>\n<p>Various additions to the JDK:<\/p>\n<ul><li>All constructors of the URL class have been deprecated, the URI class should be preferred over the URL class and, if necessary, usage of <code>URI.toURL()<\/code> to construct a URL object from a URI.<\/li>\n\n<li>Matcher.hasMatch(), MatchResult.end(String), MatchResult.group(String), MatchResult.start(String)<\/li>\n\n<li><code>Float.float16ToFloat(short)<\/code>: Returns the float value closest to the numeric value of the argument, which is a binary16 floating point value encoded in a short.<\/li>\n\n<li><code>Float.floatToFloat16(float)<\/code>: Returns the binary16 floating point value, encoded in the nearest short of the argument.<\/li>\n<\/ul>\n<p>All new JDK 20 APIs can be found in <a href=\"https:\/\/javaalmanac.io\/jdk\/20\/apidiff\/19\/\" rel=\"noopener\" target=\"_blank\">The Java Version Almanac \u2013 New APIs in Java 20<\/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 intrinsic methods), and security improvements.<\/p>\n<p>On the performance side, we can note several improvements in the management of I\/O via the <code>BufferedInputStream<\/code>, <code>PushbackInputStream<\/code>, <code>SequenceInputStream<\/code> and <code>ZipInputStream<\/code> classes.<\/p>\n<p>Thomas Schatzl contributed an improvement to the G1 garbage collector which now uses only one bitmap to store the liveness information of the heap objects. This change reduces the native memory size used by G1 by exactly 1.5% of the heap size. More information in this article which also contains a complete reminder of the different phases of G1: <a href=\"https:\/\/tschatzl.github.io\/2022\/08\/04\/concurrent-marking.html\" rel=\"noopener\" target=\"_blank\">Concurrent Marking in G1<\/a>. Other changes have been made on the Garbage Collector side, you can find them in this article by Thomas Schatzl: <a href=\"https:\/\/tschatzl.github.io\/2023\/03\/14\/jdk20-g1-parallel-gc-changes.html\" rel=\"noopener\" target=\"_blank\">JDK 20 G1\/Parallel\/Serial GC changes<\/a>.<\/p>\n<p>On the security side, the focus has been on strengthening JVM security, cryptographic algorithm performance, as well as adding JFR events for security monitoring. You can refer to Sean Mullan&#8217;s article for an exhaustive list of security changes included in this release: <a href=\"https:\/\/seanjmullan.org\/blog\/2023\/03\/22\/jdk20\" rel=\"noopener\" target=\"_blank\">JDK 20 Security Enhancements<\/a>.<\/p>\n<h2>Conclusion<\/h2>\n<p>This new version brings some minor changes to pattern matching, and some improvements in concurrent programming via the new Scoped Values API. So very little compared to the previous versions. The next release will also be the next LTS, I hope for this one, that some of the JEP that have been in preview for literally years, will be promoted to stable.<\/p>","protected":false},"excerpt":{"rendered":"<p>Now that Java 20 is features complete (Rampdown Phase One at the day of writing), it\u2019s time to walk through all the functionalities that bring 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 19, Java 18, Java 17, Java 16, Java 15, Java 14, Java 13, Java 12, Java 11,\u00a0Java 10,\u00a0and\u00a0Java 9. We&#8230;<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/www.loicmathieu.fr\/wordpress\/informatique\/java-20-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,209,163],"class_list":["post-1572","post","type-post","status-publish","format-standard","hentry","category-informatique","tag-java","tag-java20","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":1572,"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":1572,"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":1572,"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":1572,"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":1572,"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":1572,"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\/1572","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=1572"}],"version-history":[{"count":23,"href":"https:\/\/www.loicmathieu.fr\/wordpress\/wp-json\/wp\/v2\/posts\/1572\/revisions"}],"predecessor-version":[{"id":1694,"href":"https:\/\/www.loicmathieu.fr\/wordpress\/wp-json\/wp\/v2\/posts\/1572\/revisions\/1694"}],"wp:attachment":[{"href":"https:\/\/www.loicmathieu.fr\/wordpress\/wp-json\/wp\/v2\/media?parent=1572"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.loicmathieu.fr\/wordpress\/wp-json\/wp\/v2\/categories?post=1572"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.loicmathieu.fr\/wordpress\/wp-json\/wp\/v2\/tags?post=1572"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}