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.
Comments
robse replied ago:
"".equals(s)
nice to know
kdavies replied ago:
"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.
jwenting replied ago:
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...
Voters For This Link (13)
Voters Against This Link (0)