• Post Reply Bookmark Topic Watch Topic
  • New Topic

at the job,do NOT excessive using of String  RSS feed

 
Alan Hermin
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Joe Ess
Bartender
Posts: 9439
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chengwei Lee
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joe,

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.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joe Ess
Bartender
Posts: 9439
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I totally agree with Tony on this one.

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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!