tag:blogger.com,1999:blog-23939980.post115521401843141604..comments2023-09-25T17:29:29.440+10:00Comments on Java and other things: Empty StringAnonymoushttp://www.blogger.com/profile/11568089987006666223noreply@blogger.comBlogger13125tag:blogger.com,1999:blog-23939980.post-63336214736102343182011-07-15T06:41:16.669+10:002011-07-15T06:41:16.669+10:00String class has a special method
isEmpty() that i...String class has a special method<br />isEmpty() that is to be used for empty comparison. It is way better than s.length() == 0 because:<br />- Less noisy<br />- Explicitly tells what you want<br />- Delegates efficient implementation to the class String that knows better how to implement it efficiently<br /><br />BTW this is the implementation:<br /><br />public boolean isEmpty() {<br /> return count == 0;<br />}<br /><br />Writing s.length == 0 is efficient but not a good style.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-23939980.post-89256405821929912982009-11-24T09:19:50.564+11:002009-11-24T09:19:50.564+11:00Well,it will throw null pointer exception...Well,it will throw null pointer exception...Addynoreply@blogger.comtag:blogger.com,1999:blog-23939980.post-48436615154203050242009-10-22T09:26:49.868+11:002009-10-22T09:26:49.868+11:00Maybe the VM could/should optimize s.equals("...Maybe the VM could/should optimize s.equals("") to s.length() == 0.Adam Monsenhttps://www.blogger.com/profile/18129153812733370245noreply@blogger.comtag:blogger.com,1999:blog-23939980.post-64217506332285270452008-12-06T23:16:00.000+11:002008-12-06T23:16:00.000+11:00This comment has been removed by a blog administrator.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-23939980.post-25716732886043095962008-08-22T18:18:00.000+10:002008-08-22T18:18:00.000+10:00Sure! Libraries are all about code re-usability. S...Sure! Libraries are all about code re-usability. See my <A HREF="http://hanuska.blogspot.com/2006/03/how-much-code-can-be-re-used.html" REL="nofollow">How much code can be re-used?</A> post for more information.Anonymoushttps://www.blogger.com/profile/11568089987006666223noreply@blogger.comtag:blogger.com,1999:blog-23939980.post-27357983708681337792008-08-22T15:06:00.000+10:002008-08-22T15:06:00.000+10:00how abt using the StringUtils class and using the ...how abt using the StringUtils class and using the isBlank method which takes care of null,empty and whitespace?<BR/>Which usage is better?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-23939980.post-35719560824485802732008-04-09T21:09:00.000+10:002008-04-09T21:09:00.000+10:00Nothing stops you from doing thisif (s == null || ...Nothing stops you from doing this<BR/><BR/>if (s == null || s.length() == 0) ...Anonymoushttps://www.blogger.com/profile/11568089987006666223noreply@blogger.comtag:blogger.com,1999:blog-23939980.post-22218238413349292902008-04-09T11:50:00.000+10:002008-04-09T11:50:00.000+10:00But wouldn't length() return a NullPointerExceptio...But wouldn't length() return a NullPointerException if the string turns out to be null?Jonihttps://www.blogger.com/profile/04349406824750841428noreply@blogger.comtag:blogger.com,1999:blog-23939980.post-76146560147340070342007-09-28T13:20:00.000+10:002007-09-28T13:20:00.000+10:00Well, I did not like the way he posted it (he did ...Well, I did not like the way he posted it <I>(he did not ask for permission)</I>, but fairly enough he placed a link to my original post at the end.<BR/><BR/>Besides, I put some more links pointing to my blog in the comment I left on his page, which resulted in more traffic being driven my way anyway.<BR/><BR/>The comment you left here just proves it :-)Anonymoushttps://www.blogger.com/profile/11568089987006666223noreply@blogger.comtag:blogger.com,1999:blog-23939980.post-77709208345038274502007-09-28T03:55:00.000+10:002007-09-28T03:55:00.000+10:00Wow. I thought you were kidding when ya thanked th...Wow. I thought you were kidding when ya thanked that guy for quoting your entire article.Jay Bosehttps://www.blogger.com/profile/03176537559779277355noreply@blogger.comtag:blogger.com,1999:blog-23939980.post-19788620396218403932007-07-02T20:21:00.000+10:002007-07-02T20:21:00.000+10:00Thanks for sharing.Thanks for sharing.Kamalhttps://www.blogger.com/profile/03241660835422874258noreply@blogger.comtag:blogger.com,1999:blog-23939980.post-89527448384098438462007-02-23T02:54:00.000+11:002007-02-23T02:54:00.000+11:00Thanks for the discription regarding the empty str...Thanks for the discription regarding the empty string .It was Very useful .Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-23939980.post-41230630009489733922006-10-26T00:49:00.000+10:002006-10-26T00:49:00.000+10:00Thanks for the explanation on the empty comparison...Thanks for the explanation on the empty comparison ! Exactly what i was looking for :)Anonymousnoreply@blogger.com