Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #7591

Re: StringBuilder

Date 2011-09-05 19:34 -0400
From Arne Vajhøj <arne@vajhoej.dk>
Newsgroups comp.lang.java.programmer
Subject Re: StringBuilder
References <96f358c8-a024-40db-b60b-300186c2f813@o10g2000vby.googlegroups.com> <j41fik$3qb$1@news.albasani.net>
Message-ID <4e655caa$0$308$14726298@news.sunsite.dk> (permalink)
Organization SunSITE.dk - Supporting Open source

Show all headers | View raw


On 9/4/2011 11:27 PM, Jan Burse wrote:
> bob schrieb:
>> Is there any way to use StringBuilder but with + signs instead of
>> append?
>
> The + in Java does already internally use StringBuilder.
>
> There is only an issue when you want to accumulate a string
> value. If you then explicitly use StringBuilder you are
> faster, because you save the new StringBuilder() and toString().
>
> So this is faster, since it uses 1 new and 1 toString():
>
> StringBuilder buf=new StringBuilder();
> for (int i=0; i<100; i++) {
> buf.append(i);
> buf("*");
> buf.append(i);
> buf.append("=");
> buf.append((i*i));
> buf.append("\n");
> }
> System.out.println(buf.toString());
>
> Whereby this code is slower:
>
> String res="";
> for (int i=0; i<100; i++) {
> res+=i+"*"+i+"="+(i*i)+"\n";
> }
> System.out.println(res);
>
> It is translated to the following code by the compiler, and
> thus uses 100 new and 100 toString():
>
> String res="";
> for (int i=0; i<100; i++) {
> StringBuilder _buf=new StringBuilder(res);
> _buf.append(i);
> _buf("*");
> _buf.append(i);
> _buf.append("=");
> _buf.append((i*i));
> _buf.append("\n");
> res=_buf.toString();
> }
> System.out.println(res);
>
> For more information see for example here:
> http://caprazzi.net/posts/java-bytecode-string-concatenation-and-stringbuilder/

That has been known for 10-15 years.

It should be in any Java book above beginners level.

Arne

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

StringBuilder bob <bob@coolgroups.com> - 2011-09-04 19:45 -0700
  Re: StringBuilder Arne Vajhøj <arne@vajhoej.dk> - 2011-09-04 22:53 -0400
  Re: StringBuilder Jan Burse <janburse@fastmail.fm> - 2011-09-05 05:27 +0200
    Re: StringBuilder Arne Vajhøj <arne@vajhoej.dk> - 2011-09-05 19:34 -0400
      Re: StringBuilder Wanja Gayk <brixomatic@yahoo.com> - 2011-09-11 16:35 +0200
        Re: StringBuilder Arne Vajhøj <arne@vajhoej.dk> - 2011-09-11 16:48 -0400
    Re: StringBuilder Roedy Green <see_website@mindprod.com.invalid> - 2011-09-05 17:01 -0700
  Re: StringBuilder Roedy Green <see_website@mindprod.com.invalid> - 2011-09-05 11:08 -0700
    Re: StringBuilder Jan Burse <janburse@fastmail.fm> - 2011-09-05 23:54 +0200
      Re: StringBuilder Jan Burse <janburse@fastmail.fm> - 2011-09-06 00:06 +0200
      Re: StringBuilder Roedy Green <see_website@mindprod.com.invalid> - 2011-09-05 17:03 -0700
        Re: StringBuilder Jan Burse <janburse@fastmail.fm> - 2011-09-06 08:53 +0200

csiph-web