{"id":12568,"date":"2019-07-30T07:25:35","date_gmt":"2019-07-30T05:25:35","guid":{"rendered":"https:\/\/www.dbi-services.com\/blog\/java-jdk12-jep-325-switch-expressions\/"},"modified":"2019-07-30T07:25:35","modified_gmt":"2019-07-30T05:25:35","slug":"java-jdk12-jep-325-switch-expressions","status":"publish","type":"post","link":"https:\/\/www.dbi-services.com\/blog\/java-jdk12-jep-325-switch-expressions\/","title":{"rendered":"Java JDK12: JEP 325: Switch Expressions"},"content":{"rendered":"<h1>Eclipse setup<\/h1>\n<p>In order to test Java JDK12 you will have to download Eclipse 4.11 at least. Then download JDK12 from Oracle&#8217;s web site. And configure Eclipse to use this JDK for the project. Also download the JDK12 support with the following repository link:<\/p>\n<p><code>https:\/\/download.eclipse.org\/eclipse\/updates\/4.11-P-builds<\/code><\/p>\n<p>Edit the projects properties to use the &#8220;Java Compiler&#8221; compliance 12. And &#8220;Enable preview features&#8221; set to TRUE. Now you should be able to run JDK12 examples. Here a link for the complete setup:<\/p>\n<p><code>https:\/\/marketplace.eclipse.org\/content\/java-12-support-eclipse-2019-03-411<\/code><\/p>\n<h1>Switch expression<\/h1>\n<p>The switch expression receives a new lifting in this version. It allows more readiness and more flexibility. We can now:<\/p>\n<ul>\n<li>Get rid of the break word in certain cases<\/li>\n<li>Return a value from the switch<\/li>\n<li>Put several values in one &#8220;case&#8221;<\/li>\n<\/ul>\n<p>With the following example you can see how to get rid of the &#8220;break&#8221; and use a &#8220;case L -&gt;&#8221; switch label. In addition, the switch returns a value which can be used after the expression.<\/p>\n<pre class=\"brush: bash; gutter: true; first-line: 1\">String alias = switch (day) {\n    case MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY \t-&gt; \"Week day\";\n    case SATURDAY, SUNDAY\t\t\t\t                -&gt; \"Weekend\";\n};\nSystem.out.println(alias);\n<\/pre>\n<p>The previous few lines replaces the following ones:<\/p>\n<pre class=\"brush: bash; gutter: true; first-line: 1\">\nswitch (day) {\n    case MONDAY:\n        System.out.println(\"Week day\");\n        break;\n    case TUESDAY:\n        System.out.println(\"Week day\");\n        break;\n    case WEDNESDAY:\n        System.out.println(\"Week day\");\n        break;\n    case THURSDAY:\n        System.out.println(\"Week day\");\n        break;\n    case FRIDAY:\n        System.out.println(\"Week day\");\n        break;\n    case SATURDAY:\n        System.out.println(\"Weekend\");\n        break;\n    case SUNDAY:\n        System.out.println(\"Weekend\");\n        break;\n}\n<\/pre>\n<p>Here the expression:<\/p>\n<pre class=\"brush: bash; gutter: true; first-line: 1\">\nT result = switch (arg) {\n    case L1 -&gt; e1;\n    case L2 -&gt; e2;\n    default -&gt; e3;\n};\n<\/pre>\n<p>When using a block of code you will still need a &#8220;break&#8221;:<\/p>\n<pre class=\"brush: bash; gutter: true; first-line: 1\">\nint i = switch (day) {\n     case MONDAY -&gt; {\n     System.out.println(\"Monday\");\n     break 0; \/\/break is needed here to return a value if switch need a return\n     }\n     default -&gt; 1;\n}\n<\/pre>\n<p>Thanks to this new notation, we can now return values with a SWITCH expression. In addition it makes it more readable fo big switch expressions. Note that you can still use the &#8220;old&#8221; way, thus old code will still compile with the new JDK. For the moment it&#8217;s deployed in JDK12 which is a non-LTS release, but if the new expression is validated by the tests and results, it will definitely be part of future releases like the LTS versions and you will be able to use it then.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Eclipse setup In order to test Java JDK12 you will have to download Eclipse 4.11 at least. Then download JDK12 from Oracle&#8217;s web site. And configure Eclipse to use this JDK for the project. Also download the JDK12 support with the following repository link: https:\/\/download.eclipse.org\/eclipse\/updates\/4.11-P-builds Edit the projects properties to use the &#8220;Java Compiler&#8221; compliance [&hellip;]<\/p>\n","protected":false},"author":40,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[197],"tags":[1638,214,1639,1640,754],"type_dbi":[],"class_list":["post-12568","post","type-post","status-publish","format-standard","hentry","category-application-integration-middleware","tag-expression","tag-java","tag-jdk12","tag-jep325","tag-switch"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.2 (Yoast SEO v27.5) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Java JDK12: JEP 325: Switch Expressions - dbi Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.dbi-services.com\/blog\/java-jdk12-jep-325-switch-expressions\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java JDK12: JEP 325: Switch Expressions\" \/>\n<meta property=\"og:description\" content=\"Eclipse setup In order to test Java JDK12 you will have to download Eclipse 4.11 at least. Then download JDK12 from Oracle&#8217;s web site. And configure Eclipse to use this JDK for the project. Also download the JDK12 support with the following repository link: https:\/\/download.eclipse.org\/eclipse\/updates\/4.11-P-builds Edit the projects properties to use the &#8220;Java Compiler&#8221; compliance [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.dbi-services.com\/blog\/java-jdk12-jep-325-switch-expressions\/\" \/>\n<meta property=\"og:site_name\" content=\"dbi Blog\" \/>\n<meta property=\"article:published_time\" content=\"2019-07-30T05:25:35+00:00\" \/>\n<meta name=\"author\" content=\"Middleware Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Middleware Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/java-jdk12-jep-325-switch-expressions\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/java-jdk12-jep-325-switch-expressions\\\/\"},\"author\":{\"name\":\"Middleware Team\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#\\\/schema\\\/person\\\/8d8563acfc6e604cce6507f45bac0ea1\"},\"headline\":\"Java JDK12: JEP 325: Switch Expressions\",\"datePublished\":\"2019-07-30T05:25:35+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/java-jdk12-jep-325-switch-expressions\\\/\"},\"wordCount\":275,\"commentCount\":0,\"keywords\":[\"expression\",\"Java\",\"jdk12\",\"jep325\",\"SWITCH\"],\"articleSection\":[\"Application integration &amp; Middleware\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/java-jdk12-jep-325-switch-expressions\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/java-jdk12-jep-325-switch-expressions\\\/\",\"url\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/java-jdk12-jep-325-switch-expressions\\\/\",\"name\":\"Java JDK12: JEP 325: Switch Expressions - dbi Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#website\"},\"datePublished\":\"2019-07-30T05:25:35+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#\\\/schema\\\/person\\\/8d8563acfc6e604cce6507f45bac0ea1\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/java-jdk12-jep-325-switch-expressions\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/java-jdk12-jep-325-switch-expressions\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/java-jdk12-jep-325-switch-expressions\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Accueil\",\"item\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java JDK12: JEP 325: Switch Expressions\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/\",\"name\":\"dbi Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#\\\/schema\\\/person\\\/8d8563acfc6e604cce6507f45bac0ea1\",\"name\":\"Middleware Team\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/ddcae7ba0f9d1a0e7ae707f0e689e4a9c95bb48ec49c8e6d9cc86d43f4121cb6?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/ddcae7ba0f9d1a0e7ae707f0e689e4a9c95bb48ec49c8e6d9cc86d43f4121cb6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/ddcae7ba0f9d1a0e7ae707f0e689e4a9c95bb48ec49c8e6d9cc86d43f4121cb6?s=96&d=mm&r=g\",\"caption\":\"Middleware Team\"},\"url\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/author\\\/middleware-team\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Java JDK12: JEP 325: Switch Expressions - dbi Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.dbi-services.com\/blog\/java-jdk12-jep-325-switch-expressions\/","og_locale":"en_US","og_type":"article","og_title":"Java JDK12: JEP 325: Switch Expressions","og_description":"Eclipse setup In order to test Java JDK12 you will have to download Eclipse 4.11 at least. Then download JDK12 from Oracle&#8217;s web site. And configure Eclipse to use this JDK for the project. Also download the JDK12 support with the following repository link: https:\/\/download.eclipse.org\/eclipse\/updates\/4.11-P-builds Edit the projects properties to use the &#8220;Java Compiler&#8221; compliance [&hellip;]","og_url":"https:\/\/www.dbi-services.com\/blog\/java-jdk12-jep-325-switch-expressions\/","og_site_name":"dbi Blog","article_published_time":"2019-07-30T05:25:35+00:00","author":"Middleware Team","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Middleware Team","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.dbi-services.com\/blog\/java-jdk12-jep-325-switch-expressions\/#article","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/java-jdk12-jep-325-switch-expressions\/"},"author":{"name":"Middleware Team","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/8d8563acfc6e604cce6507f45bac0ea1"},"headline":"Java JDK12: JEP 325: Switch Expressions","datePublished":"2019-07-30T05:25:35+00:00","mainEntityOfPage":{"@id":"https:\/\/www.dbi-services.com\/blog\/java-jdk12-jep-325-switch-expressions\/"},"wordCount":275,"commentCount":0,"keywords":["expression","Java","jdk12","jep325","SWITCH"],"articleSection":["Application integration &amp; Middleware"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.dbi-services.com\/blog\/java-jdk12-jep-325-switch-expressions\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.dbi-services.com\/blog\/java-jdk12-jep-325-switch-expressions\/","url":"https:\/\/www.dbi-services.com\/blog\/java-jdk12-jep-325-switch-expressions\/","name":"Java JDK12: JEP 325: Switch Expressions - dbi Blog","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/#website"},"datePublished":"2019-07-30T05:25:35+00:00","author":{"@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/8d8563acfc6e604cce6507f45bac0ea1"},"breadcrumb":{"@id":"https:\/\/www.dbi-services.com\/blog\/java-jdk12-jep-325-switch-expressions\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.dbi-services.com\/blog\/java-jdk12-jep-325-switch-expressions\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.dbi-services.com\/blog\/java-jdk12-jep-325-switch-expressions\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Accueil","item":"https:\/\/www.dbi-services.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Java JDK12: JEP 325: Switch Expressions"}]},{"@type":"WebSite","@id":"https:\/\/www.dbi-services.com\/blog\/#website","url":"https:\/\/www.dbi-services.com\/blog\/","name":"dbi Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.dbi-services.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/8d8563acfc6e604cce6507f45bac0ea1","name":"Middleware Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/ddcae7ba0f9d1a0e7ae707f0e689e4a9c95bb48ec49c8e6d9cc86d43f4121cb6?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/ddcae7ba0f9d1a0e7ae707f0e689e4a9c95bb48ec49c8e6d9cc86d43f4121cb6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ddcae7ba0f9d1a0e7ae707f0e689e4a9c95bb48ec49c8e6d9cc86d43f4121cb6?s=96&d=mm&r=g","caption":"Middleware Team"},"url":"https:\/\/www.dbi-services.com\/blog\/author\/middleware-team\/"}]}},"_links":{"self":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/12568","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/users\/40"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/comments?post=12568"}],"version-history":[{"count":0,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/12568\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/media?parent=12568"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/categories?post=12568"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/tags?post=12568"},{"taxonomy":"type","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/type_dbi?post=12568"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}