Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » J2EE Interest »

Re: String concatenation

Tim Wood

2005-08-31


At 01:45 PM 08/31/05, you wrote:
>Hi,
>I have a question in string concatenation.
>
>I have the below code.
>
>StringBuffer a = new StringBuffer();
>a.append("Name=" + userVo.getName()); ...
>
>Instead of using + to append strings, if I concatenate strings as below,
>will it be faster than the above one?
>
>StringBuffer a = new StringBuffer();
>a.append("Name=").append(userVo.getName())); ...


Yes, the 2nd way should be faster. That's how I do it. It's my understanding that the + operator will compile into a temp. StringBuffer creation and append()s of the operands in the sum, then a toString() (unless they're known at compile time, then the compiler can compute the sum).
HTH,
TW

===========================================================================
To unsubscribe, send email to listserv@(protected)
of the message "signoff J2EE-INTEREST". For general help, send email to
listserv@(protected)".
©2008 junlu.com - Jax Systems, LLC, U.S.A.