The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
January 2002



This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


So that's the major difference between String and StringBuffer!!!! (eom)

Posted by Hiran on January 24, 2002 at 7:37 PM

> > Instead of:
> > String reversed;
> > Try this instead:
> > String reversed = "";

> By the way, even if you are not going to use StringBuffer.reverse(), you should still use a StringBuffer class, not a String. If you keep changing a String, what you are really doing is creating a plethora of slightly different Strings, since each String object is immutable. This is really horribly inefficient code and is the whole reason that StringBuffer exists. So what you should do is start out with an empty StringBuffer and append chars from the String (working backwards, as mentioned), then return StringBuffer.toString().


Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us