{"id":1369,"date":"2011-04-25T09:01:24","date_gmt":"2011-04-25T16:01:24","guid":{"rendered":"http:\/\/juixe.com\/techknow\/?p=1369"},"modified":"2011-04-24T11:26:40","modified_gmt":"2011-04-24T18:26:40","slug":"java-string-conversion-puzzlers","status":"publish","type":"post","link":"http:\/\/juixe.com\/techknow\/index.php\/2011\/04\/25\/java-string-conversion-puzzlers\/","title":{"rendered":"Java String Conversion Puzzlers"},"content":{"rendered":"<p>In Java, object coercion from one type to another can led to interesting results, and possible bugs when done implicitly.  For example, a common type conversion bug is when you have a method that accepts a primitive boolean but pass it an an object of type Boolean.  If the object is null, the conversion from a Boolean object to a boolean primitive will cause a NullPointerException.<\/p>\n<p>But mixing between Strings, characters, and integers can result in an interesting mix of results.  For example, I think that any Java developer would have some doubt in describing the output from the following Java code.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nSystem.out.println(&quot;Hello, &quot;+(char)('A'+1));\r\nSystem.out.println(&quot;Hello, &quot;+('A'+1));\r\nSystem.out.println(&quot;Hello, &quot;+'A'+1);\r\n<\/pre>\n<p>The one rule to remember about coercion is that if you are performing a calculation with two different types, the least accurate type will be coerced or converted to the more accurate.  In this case, adding a character and an integer will result in converting the character to the corresponding integer value before performing the addition operation.  Because the integer equivalent of the char value &#8216;A&#8217; is 65, then &#8216;A&#8217;+1 = 66.  When adding between a string, character, and integer the character and integer are converted to strings because concatenating the values into a new string.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n&quot;Hello, &quot;+(char)('A'+1); \/\/ Hello, B\r\n&quot;Hello, &quot;+('A'+1); \/\/ Hello, 66\r\n&quot;Hello, &quot;+'A'+1; \/\/ Hello, A1\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In Java, object coercion from one type to another can led to interesting results, and possible bugs when done implicitly. For example, a common type conversion bug is when you have a method that accepts a primitive boolean but pass it an an object of type Boolean. If the object is null, the conversion from [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[15,19,3],"tags":[42,30,503,502,500,504,501],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p902K-m5","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/1369"}],"collection":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/comments?post=1369"}],"version-history":[{"count":2,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/1369\/revisions"}],"predecessor-version":[{"id":1372,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/1369\/revisions\/1372"}],"wp:attachment":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/media?parent=1369"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/categories?post=1369"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/tags?post=1369"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}