{"id":41,"date":"2022-10-17T19:16:21","date_gmt":"2022-10-17T19:16:21","guid":{"rendered":"https:\/\/www.richardlipka.com\/blog\/?p=41"},"modified":"2022-10-17T19:16:24","modified_gmt":"2022-10-17T19:16:24","slug":"how-to-compare-enums-in-java","status":"publish","type":"post","link":"https:\/\/www.richardlipka.com\/thoughts\/index.php\/2022\/10\/17\/how-to-compare-enums-in-java\/","title":{"rendered":"How to compare Enums in Java"},"content":{"rendered":"\n<p>Hello guys.<\/p>\n\n\n\n<p>I would like to share with you a small hint on how to compare Enums in Java. I see a lot of people who compare enums in java incorrectly.<\/p>\n\n\n\n<p>Let&#8217;s say that we have Animal enumeration.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>enum<\/strong> Animal {\n   DOG, CAT, BIRD\n}<\/pre>\n\n\n\n<p>The worst way is to compare enums like the example below.  This type of comparison can throw <em>NullPointerException<\/em> in case a variable <em>animal <\/em>is <strong>null<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>if<\/strong> (animal.equals(Animal.BIRD)) {\n     \/\/DO SOMETHING\n}<\/pre>\n\n\n\n<p>A better way how to compare java is to switch variable with the Enum constant.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>if<\/strong> (Animal.BIRD.equals(animal)) {\n     \/\/DO SOMETHING\n}<\/pre>\n\n\n\n<p>The best way of comparison Enums in Java is to use<strong> == <\/strong>operator. The reason why to use this operator is that your code will be null-safe. Check the Java code below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>if<\/strong> (Animal.BIRD == animal) {\n     \/\/DO SOMETHING\n}<\/pre>\n\n\n\n<p>I hope that this article help someone.<\/p>\n\n\n\n<p>Richard<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello guys. I would like to share with you a small hint on how to compare Enums in Java. I see a lot of people who compare enums in java incorrectly. Let&#8217;s say that we have Animal enumeration. enum Animal { DOG, CAT, BIRD } The worst way is to compare enums like the example [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-41","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.richardlipka.com\/thoughts\/index.php\/wp-json\/wp\/v2\/posts\/41","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.richardlipka.com\/thoughts\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.richardlipka.com\/thoughts\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.richardlipka.com\/thoughts\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.richardlipka.com\/thoughts\/index.php\/wp-json\/wp\/v2\/comments?post=41"}],"version-history":[{"count":1,"href":"https:\/\/www.richardlipka.com\/thoughts\/index.php\/wp-json\/wp\/v2\/posts\/41\/revisions"}],"predecessor-version":[{"id":42,"href":"https:\/\/www.richardlipka.com\/thoughts\/index.php\/wp-json\/wp\/v2\/posts\/41\/revisions\/42"}],"wp:attachment":[{"href":"https:\/\/www.richardlipka.com\/thoughts\/index.php\/wp-json\/wp\/v2\/media?parent=41"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.richardlipka.com\/thoughts\/index.php\/wp-json\/wp\/v2\/categories?post=41"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.richardlipka.com\/thoughts\/index.php\/wp-json\/wp\/v2\/tags?post=41"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}