{"id":319,"date":"2014-10-14T10:01:35","date_gmt":"2014-10-14T16:01:35","guid":{"rendered":"http:\/\/sha.nnoncarey.com\/blog\/?p=319"},"modified":"2016-03-04T17:44:26","modified_gmt":"2016-03-04T23:44:26","slug":"implicit-conversion-of-strings","status":"publish","type":"post","link":"https:\/\/sha.nnoncarey.com\/blog\/archives\/319","title":{"rendered":"Implicit conversion of Strings"},"content":{"rendered":"<p>I recently replaced some code that looked like:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">return &quot;&quot; + str;<\/pre>\n<p>with:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">return str.toString();<\/pre>\n<p>I thought I was fixing bad code style. Unfortunately, I forgot how implicit conversion to String worked, and I wasn&#8217;t thorough enough. I failed to write a test to check the behavior when str is null. Contrary to my intuition, the two statements are not equivalent. According to <a href=\"http:\/\/docs.oracle.com\/javase\/specs\/jls\/se7\/html\/jls-5.html\">the Java specification<\/a> a null reference is implicitly converted to &#8220;null&#8221;. My code, on the other hand, throws a NullPointerException. I can&#8217;t help but think this is a weakness in the language design. It makes printing null references easier but it causes at least two problems:<\/p>\n<ul>\n<li>It hides the invocation of toString(). If you&#8217;re interested in a call hierarchy of toString() your IDE will need to be intelligent enough to also show you implicit casts. A call hierarchy of toString() might not be very useful anyway, but if you&#8217;re able to restrict the scope in some way, it could be handy. IntelliJ IDEA, unfortunately, fails to show implicit conversion of strings as invocations of toString()&#8230; I might suggest a feature to fix that.<\/li>\n<li>It hides significant logic from the programmer. The implicit conversion performs something like:\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nif (ref == null) {\r\n  return &quot;null&quot;;\r\n} else {\r\n  String result = ref.toString();\r\n  if (result == null) {\r\n    return &quot;null&quot;;\r\n  } else {\r\n    return result;\r\n  }\r\n}\r\n<\/pre>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>I recently replaced some code that looked like: return &quot;&quot; + str; with: return str.toString(); I thought I was fixing bad code style. Unfortunately, I forgot how implicit conversion to String worked, and I wasn&#8217;t thorough enough. I failed to write a test to check the behavior when str is null. Contrary to my intuition, &hellip; <a href=\"https:\/\/sha.nnoncarey.com\/blog\/archives\/319\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Implicit conversion of Strings&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-319","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/sha.nnoncarey.com\/blog\/wp-json\/wp\/v2\/posts\/319","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sha.nnoncarey.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sha.nnoncarey.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sha.nnoncarey.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sha.nnoncarey.com\/blog\/wp-json\/wp\/v2\/comments?post=319"}],"version-history":[{"count":5,"href":"https:\/\/sha.nnoncarey.com\/blog\/wp-json\/wp\/v2\/posts\/319\/revisions"}],"predecessor-version":[{"id":364,"href":"https:\/\/sha.nnoncarey.com\/blog\/wp-json\/wp\/v2\/posts\/319\/revisions\/364"}],"wp:attachment":[{"href":"https:\/\/sha.nnoncarey.com\/blog\/wp-json\/wp\/v2\/media?parent=319"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sha.nnoncarey.com\/blog\/wp-json\/wp\/v2\/categories?post=319"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sha.nnoncarey.com\/blog\/wp-json\/wp\/v2\/tags?post=319"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}