Milt Epstein wrote:

> On Thu, 19 Aug 1999, Ben Coppin wrote:
>
> > You don't need to allocate space for a StringBuffer....
> >
> > you just create one:
> >
> > StringBuffer sBuffer = new StringBuffer ("");
> >
> > and then append to it as much as you like (until you run out of
> > Virtual Memory I guess!)
> [ ... ]
>
> To turn this thread a bit, and connect it with some other recent
> discussion about the performance benefits of using StringBuffer's (and
> append) instead of String's (and concatenation), I've got a question
> or two.  I can understand how performance is improved that way.  But
> does that mean you should *always* use StringBuffers instead of
> Strings and concatenation?  Because those are used all over the
> place.
>

Just because you see others use string concatenation "all over the place"
doesn't mean you necessarily should.  :-)

Like everything in programming, how much to optimize is a compromise -- how
much time can you afford to invest improving performance, versus when does the
progam have to be completed.  The "gut feel" rule that I start with regarding
this topic goes kinda like this:

* If something is going to get executed just once, like a confirmation log
  message in the init() method of a servlet, go ahead and use concatenation.
  In the big scheme of things, it just doesn't matter, and the concatenate
  version is a little easier to type.

* If something is going to get executed a few times (like an error message
  in an exception handler), I will personally tend to use string buffers --
  but that's just because I've trained myself to that habit.  It still
probably
  doesn't matter, but I'm reinforcing a good habit.

* If something is going to get executed lots of times, I will *always* code
  it with string buffers instead of concatenation.

After the app is developed, then you can profile and start applying the 80/20
rule to focus on the places where you can improve performance the most.  But
coding with StringBuffer from the get-go means I can focus on where my
application design needs to be improved, without having to go back and
tediously recode all my string concatenations if I discover that this is the
big performance killer issue.

Craig McClanahan

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to