This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
Hi ranchers? while i read the sjp5 book k&B, i read th string and string processing,, then i expect that we recommend (at the job)use of StringBuffer/StringBuilder instead the use of String,to avoid the Pooling,,is'nt that true?
It is recommended to use StringBuffer/Builder rather than String concatenation because the compiler will use a StringBuffer to do the concatenation anyway. This can lead to lots of short-term StringBuffers being created and destroyed, degrading performance (read here). As with all rules, there are exceptions. There are times when using String concatenation makes sense. See here. So the answer to your question is: Use Both. It is your job to know when to use the correct class.
Originally posted by Joe Ess: It is recommended to use StringBuffer/Builder rather than String concatenation because the compiler will use a StringBuffer to do the concatenation anyway.
I don't quite agree with you on this. I thought for String concatenations, a new String object is created. Which is why, before the arrival of Tiger, the best practice was using StringBuffer for concatenation instead of String objects.
Originally posted by Chengwei Lee: I don't quite agree with you on this. I thought for String concatenations, a new String object is created. Which is why, before the arrival of Tiger, the best practice was using StringBuffer for concatenation instead of String objects.
A new String is created either way. The difference becomes important when you start to use String concatenation in a situation where the compiler will end up creating many StringBuffers where one would have been sufficient. The classic example of this is a loop with concatenation in it.
Originally posted by Chengwei Lee: I don't quite agree with you on this.
Have a look at the first article I linked to. It has listings of the byte code generated by the compiler. You can experiment yourself using the Java class decompiler, javap [ May 24, 2006: Message edited by: Joe Ess ]
Originally posted by Joe Ess: It is recommended to use StringBuffer/Builder rather than String concatenation because the compiler will use a StringBuffer to do the concatenation anyway. This can lead to lots of short-term StringBuffers being created and destroyed, degrading performance (read here). As with all rules, there are exceptions. There are times when using String concatenation makes sense. See here. So the answer to your question is: Use Both. It is your job to know when to use the correct class.
This is an over-simplification, and unfortunately, is very common. The wiki.java.net URL is also a little contrived, but touches on an important point anyway. That using anything but the String concatenation operator for constants (of any type, and not necessarily literal as the wiki erroneously suggests) will lead to a performance degradation among other things. I have just contradicted the commonly held beliefs, but it is nowhere near the portrayal of a complete story. I merely intend to point out that the simplistic points of view that you so often hear are false.
Additionally, using StringBuffers/Builders often makes the source harder to read (at least to me). In most cases, readability is several orders of magnitude more important than the small performance improvements you get by using the Buffer/Builder.
There are exceptions, such as the mentioned concatenation in a loop. But typically it doesn't pay back to improve even those until you have proved that it is your real bottleneck.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus