{"id":865,"date":"2019-01-23T16:58:55","date_gmt":"2019-01-23T14:58:55","guid":{"rendered":"http:\/\/www.loicmathieu.fr\/wordpress\/?p=865"},"modified":"2020-05-06T10:01:09","modified_gmt":"2020-05-06T08:01:09","slug":"java-12-quoi-de-neuf","status":"publish","type":"post","link":"https:\/\/www.loicmathieu.fr\/wordpress\/informatique\/java-12-quoi-de-neuf\/","title":{"rendered":"Java 12 : what&#8217;s new"},"content":{"rendered":"<p>Now that Java 12 is features complete (Rampdown Phase 2 at the day of writing), it&#8217;s time to walk throught all it&#8217;s fonctionalities that brings to us, developers, this new version.<\/p>\n<p>This article is part of a serie on what&#8217;s new on the last versions of Java, for those who wants to read the others, here are the links : <a href=\"http:\/\/www.loicmathieu.fr\/wordpress\/en\/informatique\/java-11-quoi-de-neuf\/\">Java 11<\/a>, <a href=\"http:\/\/www.loicmathieu.fr\/wordpress\/en\/informatique\/java-10-quoi-de-neuf\/\">Java 10,<\/a>&nbsp;and&nbsp;<a href=\"http:\/\/www.loicmathieu.fr\/wordpress\/en\/informatique\/les-nouveautes-de-java-9-pour-les-developeurs\/\">Java 9<\/a>.<\/p>\n<p>Force is to note that besides the switch expression, there is not a lot of new things for developers, we almost had <a href=\"https:\/\/openjdk.java.net\/jeps\/326\">Raw String<\/a> (multi-line strings) but the functionality was removed from the release at the last moment, I hope it will reappear soon &#8230;<\/p>\n<h3>JEP 325: Switch Expressions<\/h3>\n<p>This is the main new functionality of Java 12, switch expressions allows to define switch as expression in order to be able to get, on a variable, the result of the switch. The switch evolved from a simple control structure (like a set of if\/else) to an expression able to calculate a result.<\/p>\n<p>A new syntax has been created, more practicle and concise, that uses the arrow operator already used in lambdas :  &#8216;-&gt;&#8217;. We can use this new syntax in a classical switch or in a switch expression.<\/p>\n<p>This is an example of the new syntax that uses the arrow operator, you can notice that the break is no longuer needed, this new shape of switch have an implicit break :<\/p>\n<pre><code>switch (day) {\n    case MONDAY, FRIDAY, SUNDAY -&gt; System.out.println(6);\n    case TUESDAY                -&gt; System.out.println(7);\n    case THURSDAY, SATURDAY     -&gt; System.out.println(8);\n    case WEDNESDAY              -&gt; System.out.println(9);\n}<\/code><\/pre>\n<p>This other example of a switch expression calculate an integer representing the number of the day, you can notice the &#8216;;&#8217; at the end of the expression that indicates that we are no longuer in a control structure but in an expression :<\/p>\n<pre><code>int numLetters = switch (day) {\n    case MONDAY, FRIDAY, SUNDAY -&gt; 6;\n    case TUESDAY                -&gt; 7;\n    case THURSDAY, SATURDAY     -&gt; 8;\n    case WEDNESDAY              -&gt; 9;\n};<\/code><\/pre>\n<p>In this example, each case is on a single line, we have an implicit break with the variable (here an integer litteral).<\/p>\n<p>It is possible to write the cases on multiple lines, in this case we must write the break at the end of the code block with the value that the expression must return :<\/p>\n<pre><code>int j = switch (day) {\n    case MONDAY  -&gt; 0;\n    case TUESDAY -&gt; 1;\n    default      -&gt; {\n        int k = day.toString().length();\n        int result = f(k);\n        break result;\n    }\n};<\/code><\/pre>\n<p>For now, this new switch is in preview mode, to use it you must add the option <strong>&#8211;enable-preview<\/strong> to the Java command line, I hope that it will be in final on the next version of Java &#8230;<\/p>\n<p>More info on the JEP : <a href=\"https:\/\/openjdk.java.net\/jeps\/325\"><a href=\"https:\/\/openjdk.java.net\/jeps\/325\">https:\/\/openjdk.java.net\/jeps\/325<\/a><\/a><\/p>\n<h3>Various additions :<\/h3>\n<p>&#8211;&nbsp;<a href=\"https:\/\/bugs.openjdk.java.net\/browse\/JDK-8200435\"><a href=\"https:\/\/bugs.openjdk.java.net\/browse\/JDK-8200435\">https:\/\/bugs.openjdk.java.net\/browse\/JDK-8200435<\/a><\/a> <strong>String::align<\/strong> and <strong>String::indent<\/strong> : allow to align or indent a multi-line string.\n&#8211;&nbsp;<a href=\"https:\/\/bugs.openjdk.java.net\/browse\/JDK-8203442\"><a href=\"https:\/\/bugs.openjdk.java.net\/browse\/JDK-8203442\">https:\/\/bugs.openjdk.java.net\/browse\/JDK-8203442<\/a><\/a> <strong>String::transform<\/strong> : create a new String by transforming the first one with a lambda.\n&#8211;<a href=\"https:\/\/bugs.openjdk.java.net\/browse\/JDK-8202285\"> <a href=\"https:\/\/bugs.openjdk.java.net\/browse\/JDK-8202285\">https:\/\/bugs.openjdk.java.net\/browse\/JDK-8202285<\/a><\/a> :&nbsp;<strong>Files::isSameFile<\/strong> : allow to compare two files to know if they are the same (same content).\n&#8211;&nbsp;<a href=\"https:\/\/bugs.openjdk.java.net\/browse\/JDK-8205461\"><a href=\"https:\/\/bugs.openjdk.java.net\/browse\/JDK-8205461\">https:\/\/bugs.openjdk.java.net\/browse\/JDK-8205461<\/a><\/a> : <strong>Collectors::teeing<\/strong> : create a collector that is the composition of the two others.\n&#8211;&nbsp;<a href=\"https:\/\/bugs.openjdk.java.net\/browse\/JDK-8177552\"><a href=\"https:\/\/bugs.openjdk.java.net\/browse\/JDK-8177552\">https:\/\/bugs.openjdk.java.net\/browse\/JDK-8177552<\/a><\/a> :&nbsp;<strong>java.text.CompactNumberFormat<\/strong> allow to format numbers in a compact shape as defined in the norm &nbsp;<a href=\"http:\/\/unicode.org\/reports\/tr35\/tr35-numbers.html#Compact_Number_Formats\">LDML<\/a> : 1000 -&gt; 1K, 1000000 -&gt; 1M, &#8230;<\/p>\n<h3>JEP 341: Default CDS Archives<\/h3>\n<p><a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/technotes\/guides\/vm\/class-data-sharing.html\">CDS : Class Data Sharing<\/a> is a functionality of the JVM that allows to reduce the starting time of the JVM, by recording in a file the classes metadata (there representation in the JVM), in order to re-use them in the next launch of the JVM. With the JEP 341, the classes of the JDK will be pre-processed with the CDS mechanism when the JVM will be created, and loaded by default when the JVM will be started (no need to specify any arguments on the command line). So we will benefit <em>for free<\/em> of the CDS mechanism for the classes of the JDK.<\/p>\n<p>More info in the JEP: <a href=\"https:\/\/openjdk.java.net\/jeps\/341\"><a href=\"https:\/\/openjdk.java.net\/jeps\/341\">https:\/\/openjdk.java.net\/jeps\/341<\/a><\/a><\/p>\n<h3>JEP 189: Shenandoah: A Low-Pause-Time Garbage Collector<\/h3>\n<p>The new Garbage Collector (GC):  <strong>Shenandoah<\/strong>. Developed by Redhat and already included since multiple months in there JVM, it is now integrated as an experimental features in Java 12.<\/p>\n<p>Like ZGC, this is a GC that works concurrentlty to the application, and promise minimal pauses (in the ordrer of milliseconds) for heap of very large size (multiple hundreds of Gb). It targets heaps with large size and machines with multiple cores.<\/p>\n<p>To activate it :<\/p>\n<p><code>-XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC<\/code>.<\/p>\n<p>More info in the JEP:&nbsp;<a href=\"https:\/\/openjdk.java.net\/jeps\/189\"><a href=\"https:\/\/openjdk.java.net\/jeps\/189\">https:\/\/openjdk.java.net\/jeps\/189<\/a><\/a><\/p>\n<h3>JEP 346: Promptly Return Unused Committed Memory from G1<\/h3>\n<p>This functionality allow the G1 Garbage Collector to give back to the OS some memory when it no longuer needs it. For this it add a periodic GC cycle, and, based on the load average of the computer to know it&#8217;s usage, decide to return or not some memory to the OS. This functionality is disabled by default and can be enables with :<\/p>\n<p><code>-XX:G1PeriodicGCInterval=5000&amp;nbsp;-XX:G1PeriodicGCSystemLoadThreshold=1<\/code><\/p>\n<p>More info in the JEP: <a href=\"https:\/\/openjdk.java.net\/jeps\/346\"><a href=\"https:\/\/openjdk.java.net\/jeps\/346\">https:\/\/openjdk.java.net\/jeps\/346<\/a><\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>Now that Java 12 is features complete (Rampdown Phase 2 at the day of writing), it&#8217;s time to walk throught all it&#8217;s fonctionalities that brings to us, developers, this new version. This article is part of a serie on what&#8217;s new on the last versions of Java, for those who wants to read the others, here are the links : Java 11, Java 10,&nbsp;and&nbsp;Java 9. Force is to note that besides the switch expression, there is not a lot of&#8230;<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/www.loicmathieu.fr\/wordpress\/informatique\/java-12-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,164,163],"class_list":["post-865","post","type-post","status-publish","format-standard","hentry","category-informatique","tag-java","tag-java12","tag-whatsnew"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":947,"url":"https:\/\/www.loicmathieu.fr\/wordpress\/informatique\/java-13-quoi-de-neuf\/","url_meta":{"origin":865,"position":0},"title":"Java 13 : what&#8217;s new ?","author":"admin","date":"Tuesday August 13th, 2019","format":false,"excerpt":"Now that Java 13 is features complete (Release Candidate 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 wants\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":865,"position":1},"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":[]},{"id":1284,"url":"https:\/\/www.loicmathieu.fr\/wordpress\/informatique\/java-17-quoi-de-neuf\/","url_meta":{"origin":865,"position":2},"title":"Java 17 : what&#8217;s new ?","author":"admin","date":"Thursday August  5th, 2021","format":false,"excerpt":"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\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":1684,"url":"https:\/\/www.loicmathieu.fr\/wordpress\/informatique\/java-21-quoi-de-neuf\/","url_meta":{"origin":865,"position":3},"title":"Java 21: what&#8217;s new ?","author":"admin","date":"Tuesday August  8th, 2023","format":false,"excerpt":"Now that Java 21 is features complete (Rampdown Phase Two 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\u2026","rel":"","context":"In &quot;informatique&quot;","block_context":{"text":"informatique","link":"https:\/\/www.loicmathieu.fr\/wordpress\/category\/informatique\/"},"img":{"alt_text":"Java Collection API hierarchy","src":"https:\/\/i0.wp.com\/cr.openjdk.org\/~smarks\/collections\/SequencedCollectionDiagram20220216.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/cr.openjdk.org\/~smarks\/collections\/SequencedCollectionDiagram20220216.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/cr.openjdk.org\/~smarks\/collections\/SequencedCollectionDiagram20220216.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/cr.openjdk.org\/~smarks\/collections\/SequencedCollectionDiagram20220216.png?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/cr.openjdk.org\/~smarks\/collections\/SequencedCollectionDiagram20220216.png?resize=1050%2C600&ssl=1 3x, https:\/\/i0.wp.com\/cr.openjdk.org\/~smarks\/collections\/SequencedCollectionDiagram20220216.png?resize=1400%2C800&ssl=1 4x"},"classes":[]},{"id":722,"url":"https:\/\/www.loicmathieu.fr\/wordpress\/informatique\/java-10-quoi-de-neuf\/","url_meta":{"origin":865,"position":4},"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":1375,"url":"https:\/\/www.loicmathieu.fr\/wordpress\/informatique\/java-18-quoi-de-neuf\/","url_meta":{"origin":865,"position":5},"title":"Java 18 : what&#8217;s new ?","author":"admin","date":"Tuesday January  4th, 2022","format":false,"excerpt":"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\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\/865","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=865"}],"version-history":[{"count":0,"href":"https:\/\/www.loicmathieu.fr\/wordpress\/wp-json\/wp\/v2\/posts\/865\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.loicmathieu.fr\/wordpress\/wp-json\/wp\/v2\/media?parent=865"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.loicmathieu.fr\/wordpress\/wp-json\/wp\/v2\/categories?post=865"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.loicmathieu.fr\/wordpress\/wp-json\/wp\/v2\/tags?post=865"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}