Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stringbuffer vs. String class

Status
Not open for further replies.

BillyKrow

Programmer
Aug 27, 2001
67
US
Fairly new to Java and have already become quite anoid with the String vs. Stringbuffer delemna. Various sources say use Stringbuffer when you need to re-assign a string variable.
Problem - Stringbuffer class has not even close to the same fuctionality as the String class as far as methods are concerned. For example I have many occasion for the need of Trim and indexOf methods of the String class that aren't in the StringBuffer class.
Sure I can do something like:
BufferedString = BufferedString.toString().trim();
But then not only am I allocating space for a string that will have to be picked up by garbage collection but I am also re-assinging my buffered string. Defeats the purpose of using the BufferedString class. Am I wrong here? And if I'm not is the best solution to write your own methods like trim and indexOf for the BuffereString class?
 
As you've probably seen, String is a special case in Java. This is done to save resources (not just to cause you hassle, although sometimes it seems like it!), but tends to have difficulties when you do things like looping to concatonate something.

As a general rule of thumb, it is okay to use them but not in things like loops. Normally, the garbage collecion will clean up after you. My home ----> visit me for Java and Data Warehousing resources
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top