{"id":1496,"date":"2021-10-06T20:57:10","date_gmt":"2021-10-06T07:57:10","guid":{"rendered":"https:\/\/www.ronella.xyz\/?p=1496"},"modified":"2024-03-06T21:29:54","modified_gmt":"2024-03-06T08:29:54","slug":"pattern-matching-for-instanceof","status":"publish","type":"post","link":"https:\/\/www.ronella.xyz\/?p=1496","title":{"rendered":"Type Pattern Matching for instanceof"},"content":{"rendered":"<p>The <strong>pattern matching enhancement<\/strong> that <strong>conditionally extract data<\/strong> from an object if the <strong>instanceof operator was satisfied<\/strong> and <strong>assigns it to a variable<\/strong>.<\/p>\n<p>The variable in this pattern matching is following a <strong>flow scope<\/strong>. This means that the variable is in scope if it is <strong>guaranted that it can satisfies the instanceof operator<\/strong>.<\/p>\n<h2>Code Without Pattern Matching<\/h2>\n<pre><code class=\"language-java\">record Person(String firstName, String lastName) {}\n\nRecord rec = new Person(&quot;Juan&quot;, &quot;Dela Cruz&quot;);\n\n\/\/Check if rec is a Person.\nif (rec instanceof Person) {\n\n    \/\/rec can safely be casted to Person.\n    var person = (Person) rec;\n\n    System.out.println(person);\n    System.out.println(person.firstName());\n    System.out.println(person.lastName());\n}<\/code><\/pre>\n<blockquote>\n<p>Notice that <strong>casting the rec<\/strong> to Person once the check succeded is <strong>required<\/strong>.<\/p>\n<\/blockquote>\n<h2>Code With Pattern Matching<\/h2>\n<pre><code class=\"language-java\">record Person(String firstName, String lastName) {}\n\nRecord rec = new Person(&quot;Juan&quot;, &quot;Dela Cruz&quot;);\n\n\/\/If rec is a Person extract it to a variable person.\nif (rec instanceof Person person) {\n    System.out.println(person);\n    System.out.println(person.firstName());\n    System.out.println(person.lastName());\n}<\/code><\/pre>\n<blockquote>\n<p>Notice that casting is <strong>not required<\/strong> here and Person instance is <strong>extracted to person variable<\/strong>. Moreover, the <strong>scope of the variable<\/strong> is in the <strong>truth block<\/strong> of the <strong>if statement<\/strong>.<\/p>\n<\/blockquote>\n<h2>Flow Scope<\/h2>\n<h3>Example 1<\/h3>\n<pre><code class=\"language-java\">Record rec = new Person(&quot;Juan&quot;, &quot;Dela Cruz&quot;);\n\nif (!(rec instanceof Person person)) {\n    System.out.println(&quot;In human.&quot;);\n}\nelse {\n    System.out.println(person);\n    System.out.println(person.firstName());\n    System.out.println(person.lastName());\n}<\/code><\/pre>\n<blockquote>\n<p>Notice that <strong>person variable<\/strong> usage is on the <strong>else block<\/strong> of the <strong>if statement<\/strong>.<\/p>\n<\/blockquote>\n<h3>Example 2<\/h3>\n<pre><code class=\"language-java\">Record rec = new Person(&quot;Juan&quot;, &quot;Dela Cruz&quot;);\n\nif (rec instanceof Person person &amp;&amp; &quot;JUAN DELA CRUZ&quot;.equals(String.join(person.firstName(), &quot; &quot;, person.lastName().toUpperCase()))) {\n    System.out.println(&quot;You won a price&quot;);\n}<\/code><\/pre>\n<blockquote>\n<p>Notice that the <strong>person variable is usable<\/strong> after the <strong>AND <em>(&amp;&amp;)<\/em> operator<\/strong>. This is because the and operator guarantees that the instanceof operator was satisfied before evaluating the following condition. Changing it to <strong>OR <em>(||)<\/em> operator<\/strong> will be an error, since the person <strong>variable is out of scope<\/strong>.<\/p>\n<\/blockquote>\n<h2>Practical Usage<\/h2>\n<h3>equals implementation<\/h3>\n<pre><code class=\"language-java\">import java.util.Objects;\n\nrecord Person(String firstName, String lastName) {\n\n    @Override\n    public boolean equals(Object o) {\n        if (this == o) return true;\n        if (null == o) return false;\n        if (o instanceof Person person) {\n            return Objects.equals(firstName, person.firstName()) &amp;&amp; Objects.equals(lastName, person.lastName());\n        }\n        return false;\n    }\n\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The pattern matching enhancement that conditionally extract data from an object if the instanceof operator was satisfied and assigns it to a variable. The variable in this pattern matching is following a flow scope. This means that the variable is in scope if it is guaranted that it can satisfies the instanceof operator. Code Without [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[70],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/1496"}],"collection":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1496"}],"version-history":[{"count":2,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/1496\/revisions"}],"predecessor-version":[{"id":1749,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/1496\/revisions\/1749"}],"wp:attachment":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1496"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1496"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1496"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}