{"id":1941,"date":"2014-12-01T18:19:36","date_gmt":"2014-12-01T18:19:36","guid":{"rendered":"http:\/\/www.andygibson.net\/blog\/?p=1941"},"modified":"2014-11-26T11:25:39","modified_gmt":"2014-11-26T11:25:39","slug":"comparing-constants-safely","status":"publish","type":"post","link":"http:\/\/www.andygibson.net\/blog\/quickbyte\/comparing-constants-safely\/","title":{"rendered":"Comparing Constants Safely"},"content":{"rendered":"<p>When comparing two objects, the <code>equals<\/code> method is used to return true if they are identical. Typically, this leads to the following code :<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nif (name.equals(&quot;Jim&quot;)) {\r\n}\r\n<\/pre>\n<p>The problem here is that whether intended or not, it is quite possible that the <code>name<\/code> value is null, in which case a null pointer exception would be thrown. A better practice is to execute the equals method of the string constant &#8220;Jim&#8221; instead :<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nif (&quot;Jim&quot;.equals(name)) {\r\n}\r\n<\/pre>\n<p>Since the constant is never null, a null exception will not be thrown, and if the other value is null, the equals comparison will fail.<\/p>\n<p>If you are using Java 7 or above, the new <code><a href=\"https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/util\/Objects.html\">Objects<\/a><\/code> class has an <code><a href=\"https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/util\/Objects.html#equals%28java.lang.Object,%20java.lang.Object%29\">equals<\/a><\/code> static method to compare two objects while taking <code>null<\/code> values into account.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nif (Objects.equals(name,&quot;Jim&quot;)) {\r\n}\r\n<\/pre>\n<p>Alternatively if you are using a java version prior to Java 7, but using the guava library you can use the <code><a href=\"http:\/\/docs.guava-libraries.googlecode.com\/git\/javadoc\/com\/google\/common\/base\/Objects.html\">Objects<\/a><\/code> class which has a static <code><a href=\"http:\/\/docs.guava-libraries.googlecode.com\/git\/javadoc\/com\/google\/common\/base\/Objects.html#equal%28java.lang.Object,%20java.lang.Object%29\">equal()<\/a><\/code> method that takes two objects and handles null cases for you. It should also be noted that there are probably a number of other implementations in various libraries (i.e. <a href=\"http:\/\/commons.apache.org\/proper\/commons-lang\/apidocs\/org\/apache\/commons\/lang3\/ObjectUtils.html#equals%28java.lang.Object,%20java.lang.Object%29\">Apache Commons<\/a>)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When comparing two objects, the equals method is used to return true if they are identical. Typically, this leads to the following code : The problem here is that whether intended or not, it is quite possible that the name value is null, in which case a null pointer exception would be thrown. A better [&hellip;]<\/p>\n","protected":false},"author":2,"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},"categories":[80,74],"tags":[6],"_links":{"self":[{"href":"http:\/\/www.andygibson.net\/blog\/wp-json\/wp\/v2\/posts\/1941"}],"collection":[{"href":"http:\/\/www.andygibson.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.andygibson.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.andygibson.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.andygibson.net\/blog\/wp-json\/wp\/v2\/comments?post=1941"}],"version-history":[{"count":18,"href":"http:\/\/www.andygibson.net\/blog\/wp-json\/wp\/v2\/posts\/1941\/revisions"}],"predecessor-version":[{"id":2013,"href":"http:\/\/www.andygibson.net\/blog\/wp-json\/wp\/v2\/posts\/1941\/revisions\/2013"}],"wp:attachment":[{"href":"http:\/\/www.andygibson.net\/blog\/wp-json\/wp\/v2\/media?parent=1941"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.andygibson.net\/blog\/wp-json\/wp\/v2\/categories?post=1941"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.andygibson.net\/blog\/wp-json\/wp\/v2\/tags?post=1941"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}