{"id":51,"date":"2023-02-03T10:11:23","date_gmt":"2023-02-03T10:11:23","guid":{"rendered":"https:\/\/java-online-training.de\/?p=51"},"modified":"2023-04-17T08:23:51","modified_gmt":"2023-04-17T08:23:51","slug":"java-19-pattern-matching-for-switch-expressions","status":"publish","type":"post","link":"https:\/\/java-online-training.de\/?p=51","title":{"rendered":"Java 19: Pattern Matching for switch Expressions"},"content":{"rendered":"\n<p>One of the great additions to the Java core language features was <strong>pattern matching<\/strong> introduced with <strong>Java 17<\/strong>. Pattern matching is a fancy term for a very useful new mechanic that reduces the need to cast objects and explicitly declare variables when using <strong>switch statements<\/strong> or the <strong>instanceof operator<\/strong>. <\/p>\n\n\n\n<p>As a quick refresher let&#8217;s look at a simple example for pattern matching that is valid since Java 17. <\/p>\n\n\n\n<p>As a starting point, we have a simple class hierarchy of two different birds:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" style=\"font-size:1rem;--cbp-line-number-color:#adbac7;--cbp-line-number-width:17.59375px;line-height:1.625rem\"><span role=\"button\" tabindex=\"0\" data-code=\"sealed interface Bird permits Thunderbird, Hummingbird {\n\tString twitter();\n}\n\nfinal class Hummingbird implements Bird {\n\tpublic String twitter() {\n\t\treturn &quot;hummmmm&quot;;\n\t}\n}\n\nfinal class Thunderbird implements Bird {\n\tpublic String twitter() {\n\t\treturn &quot;&quot;;\n\t}\n}\" style=\"color:#adbac7;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-dark-dimmed\" style=\"background-color: #22272e\"><code><span class=\"line\"><span style=\"color: #F47067\">sealed<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">interface<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F69D50\">Bird<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">permits<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #6CB6FF\">Thunderbird<\/span><span style=\"color: #ADBAC7\">, <\/span><span style=\"color: #6CB6FF\">Hummingbird<\/span><span style=\"color: #ADBAC7\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">\tString <\/span><span style=\"color: #DCBDFB\">twitter<\/span><span style=\"color: #ADBAC7\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F47067\">final<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">class<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F69D50\">Hummingbird<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">implements<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #6CB6FF\">Bird<\/span><span style=\"color: #ADBAC7\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">\t<\/span><span style=\"color: #F47067\">public<\/span><span style=\"color: #ADBAC7\"> String <\/span><span style=\"color: #DCBDFB\">twitter<\/span><span style=\"color: #ADBAC7\">() {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">\t\t<\/span><span style=\"color: #F47067\">return<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #96D0FF\">&quot;hummmmm&quot;<\/span><span style=\"color: #ADBAC7\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">\t}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F47067\">final<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">class<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F69D50\">Thunderbird<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">implements<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #6CB6FF\">Bird<\/span><span style=\"color: #ADBAC7\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">\t<\/span><span style=\"color: #F47067\">public<\/span><span style=\"color: #ADBAC7\"> String <\/span><span style=\"color: #DCBDFB\">twitter<\/span><span style=\"color: #ADBAC7\">() {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">\t\t<\/span><span style=\"color: #F47067\">return<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #96D0FF\">&quot;&quot;<\/span><span style=\"color: #ADBAC7\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">\t}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>In line (1) I  declare a sealed interface <em>Bird<\/em> with two possible implementations: the <em>Hummingbird <\/em>and the <em>Thunderbird<\/em>. Both of them implement the method <em>twitter <\/em>from the parent interface <em>Bird<\/em>. <\/p>\n\n\n\n<p>Next, let&#8217;s write some code that uses <strong>pattern matching<\/strong> to differentiate between those two:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" style=\"font-size:.875rem;--cbp-line-number-color:#adbac7;--cbp-line-number-width:7.703125px;line-height:1.625rem\"><span role=\"button\" tabindex=\"0\" data-code=\"public static void twitter(Bird bird) {\n\n  if (bird instanceof Hummingbird humm ) {\n\tSystem.out.println(&quot;Hummingburd twitters: &quot;+humm.twitter() );\n  } else if (bird instanceof Thunderbird thunder) {\n\tSystem.out.println(&quot;Thunderbird twitters: &quot;+thunder.twitter());\n  }\n}\" style=\"color:#adbac7;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-dark-dimmed\" style=\"background-color: #22272e\"><code><span class=\"line\"><span style=\"color: #F47067\">public<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">static<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">void<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #DCBDFB\">twitter<\/span><span style=\"color: #ADBAC7\">(Bird bird) {<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  <\/span><span style=\"color: #F47067\">if<\/span><span style=\"color: #ADBAC7\"> (bird <\/span><span style=\"color: #F47067\">instanceof<\/span><span style=\"color: #ADBAC7\"> Hummingbird humm ) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">\tSystem.out.<\/span><span style=\"color: #DCBDFB\">println<\/span><span style=\"color: #ADBAC7\">(<\/span><span style=\"color: #96D0FF\">&quot;Hummingburd twitters: &quot;<\/span><span style=\"color: #F47067\">+<\/span><span style=\"color: #ADBAC7\">humm.<\/span><span style=\"color: #DCBDFB\">twitter<\/span><span style=\"color: #ADBAC7\">() );<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  } <\/span><span style=\"color: #F47067\">else<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">if<\/span><span style=\"color: #ADBAC7\"> (bird <\/span><span style=\"color: #F47067\">instanceof<\/span><span style=\"color: #ADBAC7\"> Thunderbird thunder) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">\tSystem.out.<\/span><span style=\"color: #DCBDFB\">println<\/span><span style=\"color: #ADBAC7\">(<\/span><span style=\"color: #96D0FF\">&quot;Thunderbird twitters: &quot;<\/span><span style=\"color: #F47067\">+<\/span><span style=\"color: #ADBAC7\">thunder.<\/span><span style=\"color: #DCBDFB\">twitter<\/span><span style=\"color: #ADBAC7\">());<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>In line (3) and (5) you can see pattern matching in action. By checking the type of the bird variable by the i<strong>nstanceof <\/strong>operator, I declare a variable of the same type at the same time.  Just to see the difference, here is the<strong> pre Java 17 version<\/strong> of this code:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" style=\"font-size:.875rem;--cbp-line-number-color:#adbac7;--cbp-line-number-width:7.703125px;line-height:1.625rem\"><span role=\"button\" tabindex=\"0\" data-code=\"public static void twitterOld(Bird bird) {\n  if (bird instanceof Hummingbird) {\n    Hummingbird humm = (Hummingbird)bird;\n    System.out.println(&quot;Hummingburd twitters: &quot;+humm.twitter());\n  } else if (bird instanceof Thunderbird ) {\n    Thunderbird thunder = (Thunderbird)bird;\n    System.out.println(&quot;Thunderbird twitters: &quot;+thunder.twitter());\n  }\n}\" style=\"color:#adbac7;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-dark-dimmed\" style=\"background-color: #22272e\"><code><span class=\"line\"><span style=\"color: #F47067\">public<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">static<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">void<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #DCBDFB\">twitterOld<\/span><span style=\"color: #ADBAC7\">(Bird bird) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  <\/span><span style=\"color: #F47067\">if<\/span><span style=\"color: #ADBAC7\"> (bird <\/span><span style=\"color: #F47067\">instanceof<\/span><span style=\"color: #ADBAC7\"> Hummingbird) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    Hummingbird<\/span><span style=\"color: #F69D50\"> <\/span><span style=\"color: #ADBAC7\">humm<\/span><span style=\"color: #F69D50\"> <\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #ADBAC7\"> (Hummingbird)bird;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    System.out.<\/span><span style=\"color: #DCBDFB\">println<\/span><span style=\"color: #ADBAC7\">(<\/span><span style=\"color: #96D0FF\">&quot;Hummingburd twitters: &quot;<\/span><span style=\"color: #F47067\">+<\/span><span style=\"color: #ADBAC7\">humm.<\/span><span style=\"color: #DCBDFB\">twitter<\/span><span style=\"color: #ADBAC7\">());<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  } <\/span><span style=\"color: #F47067\">else<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">if<\/span><span style=\"color: #ADBAC7\"> (bird <\/span><span style=\"color: #F47067\">instanceof<\/span><span style=\"color: #ADBAC7\"> Thunderbird ) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    Thunderbird<\/span><span style=\"color: #F69D50\"> <\/span><span style=\"color: #ADBAC7\">thunder<\/span><span style=\"color: #F69D50\"> <\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #ADBAC7\"> (Thunderbird)bird;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    System.out.<\/span><span style=\"color: #DCBDFB\">println<\/span><span style=\"color: #ADBAC7\">(<\/span><span style=\"color: #96D0FF\">&quot;Thunderbird twitters: &quot;<\/span><span style=\"color: #F47067\">+<\/span><span style=\"color: #ADBAC7\">thunder.<\/span><span style=\"color: #DCBDFB\">twitter<\/span><span style=\"color: #ADBAC7\">());<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>In line (3) and (6) we can see the variable definition and casting that we needed although it was already clear in line 2 and 5 what type of object we were dealing with. <\/p>\n\n\n\n<p>You might think, that this would be better done in a <strong>switch statement<\/strong> and luckily, with Java 19 released, there is another preview feature that let us do exactly that. The enhancements are in detail explained in <a href=\"https:\/\/openjdk.org\/jeps\/427\">JEP 427,<\/a> so check that JEP out if you want to know all the nasty details.<\/p>\n\n\n\n<p>Let&#8217;s rewrite the above code to use the new switch statement:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" style=\"font-size:.875rem;--cbp-line-number-color:#adbac7;--cbp-line-number-width:7.703125px;line-height:1.625rem\"><span role=\"button\" tabindex=\"0\" data-code=\"public static void switchTwitter(Bird bird) {\n  switch( bird ) {\n    case Hummingbird humm -&gt; \n      System.out.println(&quot;Hummingbird twitters:&quot;+humm.twitter());\n    case Thunderbird thunder -&gt; \t\n      System.out.println(&quot;Thunderbird twitters:&quot;+thunder.twitter());\n  }  \n}\" style=\"color:#adbac7;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-dark-dimmed\" style=\"background-color: #22272e\"><code><span class=\"line\"><span style=\"color: #F47067\">public<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">static<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">void<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #DCBDFB\">switchTwitter<\/span><span style=\"color: #ADBAC7\">(Bird bird) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  <\/span><span style=\"color: #F47067\">switch<\/span><span style=\"color: #ADBAC7\">( bird ) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    <\/span><span style=\"color: #F47067\">case<\/span><span style=\"color: #ADBAC7\"> Hummingbird humm <\/span><span style=\"color: #F47067\">-&gt;<\/span><span style=\"color: #ADBAC7\"> <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">      System.out.<\/span><span style=\"color: #DCBDFB\">println<\/span><span style=\"color: #ADBAC7\">(<\/span><span style=\"color: #96D0FF\">&quot;Hummingbird twitters:&quot;<\/span><span style=\"color: #F47067\">+<\/span><span style=\"color: #ADBAC7\">humm.<\/span><span style=\"color: #DCBDFB\">twitter<\/span><span style=\"color: #ADBAC7\">());<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    <\/span><span style=\"color: #F47067\">case<\/span><span style=\"color: #ADBAC7\"> Thunderbird thunder <\/span><span style=\"color: #F47067\">-&gt;<\/span><span style=\"color: #ADBAC7\"> \t<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">      System.out.<\/span><span style=\"color: #DCBDFB\">println<\/span><span style=\"color: #ADBAC7\">(<\/span><span style=\"color: #96D0FF\">&quot;Thunderbird twitters:&quot;<\/span><span style=\"color: #F47067\">+<\/span><span style=\"color: #ADBAC7\">thunder.<\/span><span style=\"color: #DCBDFB\">twitter<\/span><span style=\"color: #ADBAC7\">());<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  }  <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>This looks a lot cleaner than the previous example using <strong>instanceof<\/strong>. You can simple test on the type of an object in the <em>case <\/em>statement and declare a local variable at the same time. The conditional code which should be executed follows the <strong>case <\/strong>in something that looks a lot like a lambda expression.  This new mechanic is also sometimes referred to as a <strong>type switch<\/strong>.<\/p>\n\n\n\n<p>But that&#8217;s not all you can do with the enhanced switch statement. Let&#8217;s expand our object model a little and introduce a method <em>age()<\/em> on the interface level, which returns the age of the bird:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" style=\"font-size:1rem;--cbp-line-number-color:#adbac7;--cbp-line-number-width:17.59375px;line-height:1.625rem\"><span role=\"button\" tabindex=\"0\" data-code=\"sealed interface Bird permits Thunderbird, Hummingbird {\n  String twitter();\n  int age();\t\n}\n\nfinal class Hummingbird implements Bird {\n\n  private int age;\n\t\n  public Hummingbird(int age) {\n    this.age = age; \n  }\n\t\n  public int age() { return age; }\n\t\n  public String twitter() { return &quot;hummmmm&quot;; }\n}\n\nfinal class Thunderbird implements Bird {\n\t\n  private int age;\n\t\n  public Thunderbird(int age) {\n    this.age = age; \n  }\n\n  public int age() { return age; }\n\n  public String twitter() { return &quot;&quot;; }\n}\" style=\"color:#adbac7;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-dark-dimmed\" style=\"background-color: #22272e\"><code><span class=\"line\"><span style=\"color: #F47067\">sealed<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">interface<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F69D50\">Bird<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">permits<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #6CB6FF\">Thunderbird<\/span><span style=\"color: #ADBAC7\">, <\/span><span style=\"color: #6CB6FF\">Hummingbird<\/span><span style=\"color: #ADBAC7\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  String <\/span><span style=\"color: #DCBDFB\">twitter<\/span><span style=\"color: #ADBAC7\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  <\/span><span style=\"color: #F47067\">int<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #DCBDFB\">age<\/span><span style=\"color: #ADBAC7\">();\t<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F47067\">final<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">class<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F69D50\">Hummingbird<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">implements<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #6CB6FF\">Bird<\/span><span style=\"color: #ADBAC7\"> {<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  <\/span><span style=\"color: #F47067\">private<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">int<\/span><span style=\"color: #F69D50\"> <\/span><span style=\"color: #ADBAC7\">age;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">\t<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  <\/span><span style=\"color: #F47067\">public<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #DCBDFB\">Hummingbird<\/span><span style=\"color: #ADBAC7\">(<\/span><span style=\"color: #F47067\">int<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F69D50\">age<\/span><span style=\"color: #ADBAC7\">) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    <\/span><span style=\"color: #6CB6FF\">this<\/span><span style=\"color: #ADBAC7\">.age <\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #ADBAC7\"> age; <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">\t<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  <\/span><span style=\"color: #F47067\">public<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">int<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #DCBDFB\">age<\/span><span style=\"color: #ADBAC7\">() { <\/span><span style=\"color: #F47067\">return<\/span><span style=\"color: #ADBAC7\"> age; }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">\t<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  <\/span><span style=\"color: #F47067\">public<\/span><span style=\"color: #ADBAC7\"> String <\/span><span style=\"color: #DCBDFB\">twitter<\/span><span style=\"color: #ADBAC7\">() { <\/span><span style=\"color: #F47067\">return<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #96D0FF\">&quot;hummmmm&quot;<\/span><span style=\"color: #ADBAC7\">; }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F47067\">final<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">class<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F69D50\">Thunderbird<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">implements<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #6CB6FF\">Bird<\/span><span style=\"color: #ADBAC7\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">\t<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  <\/span><span style=\"color: #F47067\">private<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">int<\/span><span style=\"color: #F69D50\"> <\/span><span style=\"color: #ADBAC7\">age;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">\t<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  <\/span><span style=\"color: #F47067\">public<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #DCBDFB\">Thunderbird<\/span><span style=\"color: #ADBAC7\">(<\/span><span style=\"color: #F47067\">int<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F69D50\">age<\/span><span style=\"color: #ADBAC7\">) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    <\/span><span style=\"color: #6CB6FF\">this<\/span><span style=\"color: #ADBAC7\">.age <\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #ADBAC7\"> age; <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  }<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  <\/span><span style=\"color: #F47067\">public<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">int<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #DCBDFB\">age<\/span><span style=\"color: #ADBAC7\">() { <\/span><span style=\"color: #F47067\">return<\/span><span style=\"color: #ADBAC7\"> age; }<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  <\/span><span style=\"color: #F47067\">public<\/span><span style=\"color: #ADBAC7\"> String <\/span><span style=\"color: #DCBDFB\">twitter<\/span><span style=\"color: #ADBAC7\">() { <\/span><span style=\"color: #F47067\">return<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #96D0FF\">&quot;&quot;<\/span><span style=\"color: #ADBAC7\">; }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>In this preview of the switch pattern matching feature you can check for the type of the object and <strong>also check for a boolean expression using the object at the same time<\/strong>. This is very handy if your logic depends not only on the type but different properties of your object. <\/p>\n\n\n\n<p>Here is an example that checks for the age of the birds inside the switch statement :<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" style=\"font-size:.875rem;--cbp-line-number-color:#adbac7;--cbp-line-number-width:7.703125px;line-height:1.625rem\"><span role=\"button\" tabindex=\"0\" data-code=\"public static void switchTwitterForYoungBirds(Bird bird) {\n  switch( bird ) {\n    case Hummingbird humm when humm.age() &lt; 3 -&gt; \n      System.out.println(&quot;Hummingbird twitters: &quot;+ humm.twitter() );\n    case Thunderbird thunder when thunder.age() &lt; 5 -&gt; \t\n      System.out.println(&quot;Thunderbird twitters: &quot;+thunder.twitter());\n    default -&gt; System.out.println(&quot;Bird is too old to twitter.&quot;);\n  }\n}\" style=\"color:#adbac7;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-dark-dimmed\" style=\"background-color: #22272e\"><code><span class=\"line\"><span style=\"color: #F47067\">public<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">static<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">void<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #DCBDFB\">switchTwitterForYoungBirds<\/span><span style=\"color: #ADBAC7\">(Bird bird) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  <\/span><span style=\"color: #F47067\">switch<\/span><span style=\"color: #ADBAC7\">( bird ) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    <\/span><span style=\"color: #F47067\">case<\/span><span style=\"color: #ADBAC7\"> Hummingbird humm when humm.<\/span><span style=\"color: #DCBDFB\">age<\/span><span style=\"color: #ADBAC7\">() <\/span><span style=\"color: #F47067\">&lt;<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #6CB6FF\">3<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">-&gt;<\/span><span style=\"color: #ADBAC7\"> <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">      System.out.<\/span><span style=\"color: #DCBDFB\">println<\/span><span style=\"color: #ADBAC7\">(<\/span><span style=\"color: #96D0FF\">&quot;Hummingbird twitters: &quot;<\/span><span style=\"color: #F47067\">+<\/span><span style=\"color: #ADBAC7\"> humm.<\/span><span style=\"color: #DCBDFB\">twitter<\/span><span style=\"color: #ADBAC7\">() );<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    <\/span><span style=\"color: #F47067\">case<\/span><span style=\"color: #ADBAC7\"> Thunderbird thunder when thunder.<\/span><span style=\"color: #DCBDFB\">age<\/span><span style=\"color: #ADBAC7\">() <\/span><span style=\"color: #F47067\">&lt;<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #6CB6FF\">5<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">-&gt;<\/span><span style=\"color: #ADBAC7\"> \t<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">      System.out.<\/span><span style=\"color: #DCBDFB\">println<\/span><span style=\"color: #ADBAC7\">(<\/span><span style=\"color: #96D0FF\">&quot;Thunderbird twitters: &quot;<\/span><span style=\"color: #F47067\">+<\/span><span style=\"color: #ADBAC7\">thunder.<\/span><span style=\"color: #DCBDFB\">twitter<\/span><span style=\"color: #ADBAC7\">());<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    <\/span><span style=\"color: #F47067\">default<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">-&gt;<\/span><span style=\"color: #ADBAC7\"> System.out.<\/span><span style=\"color: #DCBDFB\">println<\/span><span style=\"color: #ADBAC7\">(<\/span><span style=\"color: #96D0FF\">&quot;Bird is too old to twitter.&quot;<\/span><span style=\"color: #ADBAC7\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>In line (3) and (5) we can see a so called <strong>guarded pattern<\/strong> in action. First the variable <strong>humm <\/strong>is declared if the provided <em>Bird <\/em>object is of type <em>Hummingbird<\/em>. After this, the keyword <strong>when <\/strong>is used to start a boolean expression. This expression can also consist of more nested boolean logic as long as it evaluates to a single boolean. If the expression is true, the code after the arrow is executed. Finally, I covered all cases with older birds by a default case, that again prints some text to the console.  This reduces the required code a lot compared to nested if statements inside your case blocks. <\/p>\n\n\n\n<p>One more thing to mention is, that a type switch statement must always cover all possible types of the type hierarchy.  If you have no default clause, the compiler will throw a compile error indicating that not all cases are covered. That&#8217;s pretty neat if somebody extends a class hierarchy and forgets about updating all the type switches the hierarchy is used in.  <\/p>\n\n\n\n<p>To complete this overview, the new type switch also accepts one null value statement. Here is a small example for our bird type switch with a null case:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>It is really great that Oracle spends a good chunk of work on introducing new Java language features and tries to remove lengthy boilerplate code, that Java is so famous of.  With the improved pattern matching, Java code is much more modern, easy to read and to understand.  <\/p>\n\n\n\n<p>If you want to check out these examples yourself, you can find the code in this <a href=\"https:\/\/github.com\/java-online-training\/java-19-pattern-matching-with-switch\" data-type=\"URL\" data-id=\"https:\/\/github.com\/java-online-training\/java-19-pattern-matching-with-switch\">github repository.<\/a> <\/p>\n\n\n\n<p>The improved switch statement was released as a preview feature in Java 19. To compile and run your code with previews enabled, <a href=\"https:\/\/java-online-training.de\/?page_id=83\" data-type=\"page\" data-id=\"83\">check out this page<\/a>. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the great additions to the Java core language features was pattern matching introduced with Java 17. Pattern matching is a fancy term for a very useful new mechanic that reduces the need to cast objects and explicitly declare variables when using switch statements or the instanceof operator. As a quick refresher let&#8217;s look [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[9],"tags":[4,8,7,10],"class_list":["post-51","post","type-post","status-publish","format-standard","hentry","category-java-19","tag-java-19","tag-pattern-matching","tag-preview","tag-project-amber"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/java-online-training.de\/index.php?rest_route=\/wp\/v2\/posts\/51","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/java-online-training.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java-online-training.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java-online-training.de\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/java-online-training.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=51"}],"version-history":[{"count":20,"href":"https:\/\/java-online-training.de\/index.php?rest_route=\/wp\/v2\/posts\/51\/revisions"}],"predecessor-version":[{"id":369,"href":"https:\/\/java-online-training.de\/index.php?rest_route=\/wp\/v2\/posts\/51\/revisions\/369"}],"wp:attachment":[{"href":"https:\/\/java-online-training.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=51"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java-online-training.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=51"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java-online-training.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=51"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}