{"id":1284,"date":"2021-08-05T14:02:32","date_gmt":"2021-08-05T12:02:32","guid":{"rendered":"https:\/\/www.loicmathieu.fr\/wordpress\/?p=1284"},"modified":"2021-09-14T19:59:53","modified_gmt":"2021-09-14T17:59:53","slug":"java-17-quoi-de-neuf","status":"publish","type":"post","link":"https:\/\/www.loicmathieu.fr\/wordpress\/informatique\/java-17-quoi-de-neuf\/","title":{"rendered":"Java 17 : what&#8217;s new ?"},"content":{"rendered":"<p>Now that Java 17 is features complete (Rampdown Phase Two 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-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>This release doesn&#8217;t contain many JEPs, and therefore not many important new features. On the other hand, it brings some nice additions, and its LTS (Long Term Support) status makes it an important release.<\/p>\n<h2>JEP 406: Pattern Matching for switch (Preview)<\/h2>\n<p>This is probably the biggest news of this release, the pattern matching arrives in the switch, in preview. We can now make a switch on the type of a variable (including enum, record and array), and extract a local variable of the corresponding type.<\/p>\n<p>The JEP gives the following example with an if\/else chain:<\/p>\n<pre>\nstatic String formatter(Object o) {\n    String formatted = \"unknown\";\n    if (o instanceof Integer i) {\n        formatted = String.format(\"int %d\", i);\n    } else if (o instanceof Long l) {\n        formatted = String.format(\"long %d\", l);\n    } else if (o instanceof Double d) {\n        formatted = String.format(\"double %f\", d);\n    } else if (o instanceof String s) {\n        formatted = String.format(\"String %s\", s);\n    }\n    return formatted;\n}\n<\/pre>\n<p>Which can now be re-written with a switch expression:<\/p>\n<pre>\nstatic String formatterPatternSwitch(Object o) {\n    return switch (o) {\n        case Integer i -&gt; String.format(\"int %d\", i);\n        case Long l    -&gt; String.format(\"long %d\", l);\n        case Double d  -&gt; String.format(\"double %f\", d);\n        case String s  -&gt; String.format(\"String %s\", s);\n        default        -&gt; o.toString();\n    };\n}\n<\/pre>\n<p>In addition to supporting pattern matching, the switch now allows you to define a special <code>null<\/code> case (in its two forms: statement or expression). Previously, a null switch variable resulted in a <code>NullPointerException<\/code>. In its new form, we can add a <code>null<\/code> case to handle nulls within the switch. Without <code>case null<\/code>, the old behavior is kept, and a <code>NullPointerException<\/code> will be thrown.<\/p>\n<p>If we take the previous example, this gives us:<\/p>\n<pre>\nstatic String formatterPatternSwitch(Object o) {\n    return switch (o) {\n        case null -&gt; \"null\";\n        case Integer i -&gt; String.format(\"int %d\", i);\n        case Long l    -&gt; String.format(\"long %d\", l);\n        case Double d  -&gt; String.format(\"double %f\", d);\n        case String s  -&gt; String.format(\"String %s\", s);\n        default        -&gt; o.toString();\n    };\n}\n<\/pre>\n<p>The big advantage is that it avoids having to do a defensive test before the switch, and allows to include in it the null value as any other possible values of our variable.<\/p>\n<p>And it doesn&#8217;t stop there, the switch has been enhanced with <em>guards<\/em> that allow to include a condition to the <code>case<\/code>. The following example from the JEP shows its use. By adding a guard to the Triangle case: <code>case Triangle t &amp;&amp; (t.calculateArea() &gt; 100)<\/code>, we can create two cases: one for large triangles, and another for small ones.<\/p>\n<pre>\nstatic void testTriangle(Shape s) {\n    switch (s) {\n        case Triangle t &amp;&amp; (t.calculateArea() &gt; 100) -&gt; System.out.println(\"Large triangle\");\n        case Triangle t -&gt; System.out.println(\"Small triangle\");\n        default -&gt; System.out.println(\"Non-triangle\");\n    }\n}\n<\/pre>\n<p>More information in the <a href=\"https:\/\/openjdk.java.net\/jeps\/406\" rel=\"noopener\" target=\"_blank\">JEP-406<\/a>.<\/p>\n<h2>JEP 356: Enhanced Pseudo-Random Number Generators<\/h2>\n<p>The JEP-356 provides a new interface <code>RandomGenerator<\/code>, and a factory <code>RandomGeneratorFactory<\/code>, which provide access to a random number generator implementation. The existing generators: Random, SecureRandom, SplittableRandom and ThreadLocalRandom; now implement this interface which adds, among other things, access to a random number stream (RandomGenerator::doubles(), RandomGenerator::ints(), &#8230;).<\/p>\n<p>New random number generation algorithms have been implemented, more secure and more powerful (but they are not thread-safe anymore), they are intended to replace the old ones.<\/p>\n<p>Here are some examples of instantiation of random number generators:<\/p>\n<pre>\n\/\/ the old Random generator\nRandomGenerator rng1 = RandomGeneratorFactory.of(\"Random\").create(42);\n\/\/ the default random generator, currently jdk.random.L32X64MixRandom but this can change\nRandomGenerator rng2 = RandomGeneratorFactory.getDefault().create(42);\n\/\/ shortcut for the default\nRandomGenerator rng3 = RandomGenerator.getDefault(); \n\/\/ stream all available generators and display their names\nRandomGeneratorFactory.all().forEach(generator -&gt; System.out.println(generator.name());\n<\/pre>\n<p>More information in the <a href=\"https:\/\/openjdk.java.net\/jeps\/356\" rel=\"noopener\" target=\"_blank\">JEP-356<\/a> or in this very complete article of\nMichael Bien : <a href=\"https:\/\/mbien.dev\/blog\/entry\/enhanced-pseudo-random-number-generators\" rel=\"noopener\" target=\"_blank\">Java 17&#8217;s Enhanced Pseudo-Random Number Generators<\/a>.<\/p>\n<h2>Features that go from preview to standard<\/h2>\n<p>In Java 17, only one feature goes from preview to standard: the Sealed Classes (<a href=\"https:\/\/openjdk.java.net\/jeps\/409\" rel=\"noopener\" target=\"_blank\">JEP-409<\/a>), you can refer to my previous articles for more information.<\/p>\n<h2>Features that stay in preview<\/h2>\n<p>The following features remain in preview (or 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\/414\" rel=\"noopener\" target=\"_blank\">JEP-414<\/a> &#8211; Vector API: second incubation of this feature that incorporates improvements within the API and better performance.<\/li>\n\n<li><a href=\"https:\/\/openjdk.java.net\/jeps\/412\" rel=\"noopener\" target=\"_blank\">JEP-412<\/a> &#8211; Foreign Function &amp; Memory API: new incubator for these two features that are now linked (they use each other) within the same incubator.<\/li>\n<\/ul>\n<h2>A new port of the JVM<\/h2>\n<p>Java 17 adds support for the macOS\/AArch64 (aka Apple Silicon M1) architecture. More information about this in the <a href=\"https:\/\/openjdk.java.net\/jeps\/391\" rel=\"noopener\" target=\"_blank\">JEP-391<\/a>.<\/p>\n<h2>HexFormat<\/h2>\n<p>The class <code>java.util.HexFormat<\/code> allows conversion of primitive type, byte array, or char array to hex string and vice versa.<\/p>\n<pre>\nHexFormat.of().toHexDigits(127); \/\/ \"7f\"\nHexFormat.of().fromHexDigits(\"7f\"); \/\/ 127\n<\/pre>\n<p>More information in the ticket <a href=\"https:\/\/bugs.openjdk.java.net\/browse\/JDK-8251989\" rel=\"noopener\" target=\"_blank\">JDK-8251989<\/a>.<\/p>\n<h2>InstantSource<\/h2>\n<p>Testing code containing date manipulation has always been a challenge, especially if it uses and abuses <code>System.currentTimeMillis()<\/code>, <code>LocalDateTime.now()<\/code>, and other date initialization with the current date.<\/p>\n<p>To facilitate the testability of this kind of code, a new interface has been added to the JDK: <code>InstantSource<\/code>, with a single implementation <code>Clock<\/code>. The purpose of the <code>InstantSource<\/code> interface is to be an <code>Instant<\/code> factory. Instead of creating an <code>Instant<\/code> with the current date, you create it from the <code>InstantSource<\/code>. A test can then use an <code>InstantSource<\/code> at a fixed date instead of today&#8217;s date.<\/p>\n<p>Imagine the following code:<\/p>\n<pre>\n  public class MyBean {\n    private InstantSource source;  \/\/ dependency inject\n    ...\n    public void process(Instant endInstant) {\n      if (source.instant().isAfter(endInstant) {\n        ...\n      }\n    }\n  }\n<\/pre>\n<p>In normal operation, the <code>InstanSource<\/code> is initialized via <code>InstantSource.system()<\/code>, and in testing via <code>InstantSource.fixed(LocaDateTime.of(\/\/the hardcoded date time))<\/code>.<\/p>\n<h2>Miscellaneous<\/h2>\n<p>Various JDK additions:<\/p>\n<ul><li><code>Map.Entry.copyOf(Map.Entry)<\/code>: allows to create a copy of a Map entry that is not connected to the existing Map.<\/li>\n\n<li><code>Process.inputReader(), Process.outputWritter(), Process.errorReader()<\/code>: allows access to the input, standard output, and error output of a process via a Reader or a Writer.<\/li>\n<\/ul>\n<h2>Deprecation and encapsulation<\/h2>\n<p>Java 17 sees the deprecation for removal of the Applets API via the <a href=\"https:\/\/openjdk.java.net\/jeps\/398\" rel=\"noopener\" target=\"_blank\">JEP-398<\/a>, this one hasn&#8217;t been in use for many years, so it hasn&#8217;t caused much of a stir.<\/p>\n<p>Java 17 also sees the deprecation of the Security Manager for removal via the <a href=\"https:\/\/openjdk.java.net\/jeps\/411\" rel=\"noopener\" target=\"_blank\">JEP-411<\/a>. There was a lot of debate around this announcement, with admittedly a bit of <i>drama<\/i>, such as when Apache Netbeans announced that it couldn&#8217;t support Java 17 when all it needed to do was change a few lines of code &#8230; <br \/>\nThe removal of the Security Manager was announced, because it is complex to maintain, expensive in terms of performance, and does not provide the necessary security in the face of current challenges. It was created to secure applets which, by definition, execute <i>untrusted<\/i> code, and therefore no longer makes sense in a JVM that would no longer contain the Applet API.\nFor more info on its removal, you can read this <a href=\"https:\/\/www.infoq.com\/news\/2021\/04\/java-security-vote\/\" rel=\"noopener\" target=\"_blank\">InfoQ<\/a> article or this <a href=\"https:\/\/www.reddit.com\/r\/java\/comments\/mrkf2i\/jep_411_deprecate_the_security_manager_for_removal\/\" rel=\"noopener\" target=\"_blank\">reddit thread<\/a> (I really enjoyed pron98&#8217;s responses).<\/p>\n<p>Finally, the <a href=\"https:\/\/openjdk.java.net\/jeps\/403\" rel=\"noopener\" target=\"_blank\">JEP 403: Strongly Encapsulate JDK Internals<\/a> switched the JDK to strict encapsulation of its internal classes. This is the end of what was started with Java 9 and the modularization of the JDK.\nSpecifically, the encapsulation mode had moved from <code>--illegal-access=permit<\/code> in Java 15 to <code>--illegal-access=deny<\/code> in Java 16 with the ability to change the configuration option. With Java 17, <code>--illegal-access<\/code> disappears and access to internal JDK classes (excluding Unsafe) is no longer possible.<\/p>","protected":false},"excerpt":{"rendered":"<p>Now that Java 17 is features complete (Rampdown Phase Two 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 16, Java 15, Java 14, Java 13, Java 12, Java 11,\u00a0Java 10,\u00a0and\u00a0Java 9. This release doesn&#8217;t contain many JEPs,&#8230;<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/www.loicmathieu.fr\/wordpress\/informatique\/java-17-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,193,163],"class_list":["post-1284","post","type-post","status-publish","format-standard","hentry","category-informatique","tag-java","tag-java17","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":1284,"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":1284,"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":1284,"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":1284,"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":1284,"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":1284,"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\/1284","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=1284"}],"version-history":[{"count":0,"href":"https:\/\/www.loicmathieu.fr\/wordpress\/wp-json\/wp\/v2\/posts\/1284\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.loicmathieu.fr\/wordpress\/wp-json\/wp\/v2\/media?parent=1284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.loicmathieu.fr\/wordpress\/wp-json\/wp\/v2\/categories?post=1284"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.loicmathieu.fr\/wordpress\/wp-json\/wp\/v2\/tags?post=1284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}