Link Details

Link 44311 thumbnail
User 249548 avatar

By duzer
via itefforts.blogspot.com
Published: Sep 26 2007 / 09:02

How many times have you coded a check for String being null or empty? Countless times, right? I have. We use some ready-to-use classes from open source frameworks or we write our own StringUtils class.
  • 13
  • 0
  • 2018
  • 768

Comments

Add your comment
User 249616 avatar

robse replied ago:

1 votes Vote down Vote up Reply

"".equals(s)

nice to know

User 184684 avatar

kdavies replied ago:

0 votes Vote down Vote up Reply

"Do not concatenate Strings in loops using + operator. It's faster to use StringBuffer (or StringBuilder, which is in Tiger and is not synchronized) append() and then toString() methods instead. The plus (+) operator constructs new String object each time."

This is an often parroted piece of advise, that most the time will not make any difference at all to the performance of your application unless you are creating huge amounts of data with the + operator. I prefer to use the + most the time because it is more readable.

User 181930 avatar

jwenting replied ago:

0 votes Vote down Vote up Reply

it's actually pretty sound advise, but as you say you have to be pragmatic about it.
If you're going to do a single concattenation to a String don't go to the trouble of creating a StringBuilder, appending the original String to it, append()ing the new bit, and calling toString on it to assign things again to a String.
If you have a StringBuilder anyway, use append() religiously.
So no mixing things like builder.append("A" + "B") which you see in a lot of applications.

Always be wary of people making religious sounding statements to the effect that you should never do something in favour of something else...

Add your comment


Html tags not supported. Reply is editable for 5 minutes. Use [code lang="java|ruby|sql|css|xml"][/code] to post code snippets.

Voters For This Link (13)



Voters Against This Link (0)