[Soot-list] StringBuffer or StringBuilder when performing string concatenation?

Eric Bodden bodden at st.informatik.tu-darmstadt.de
Sat May 1 15:03:35 EDT 2010


Hello.

I think the difference may come from the fact which compiler you use
to generate the .class files. StringBuilder did not exist before Java
1.5. So when you use a Java 1.4 compiler or a later compiler with the
"-target 1.4" option then you should be getting code that uses
StringBuffer, otherwise StringBuilder. Also, StringBuilder is not
thread safe, and therefore I am not sure if compilers may actually use
it for coercion when one of the operands is a field, such as in:

String s;
void foo() { return "result is "+s);

It may well be the case that compilers will default to StringBuffer in
such cases, but I am not sure. (If s was local then multithreading
would be no problem because local variables cannot be accessed by
multiple threads.)

To solve your problem, if I was you, I would just handle both cases equally.

Eric

--
Dr. Eric Bodden
Software Technology Group, Technische Universität Darmstadt, Germany
Tel: +49 6151 16-5478    Fax: +49 6151 16-5410
Mailing Address: S2|02 A209, Hochschulstraße 10, 64289 Darmstadt



On 1 May 2010 20:44, Jinping Zhu <jnuzhjp at gmail.com> wrote:
> Hi, Eric:
>      When I tried to examine the jimple code of java string concatenation
> through "+", as to code below:
>     public void experiment(boolean condition){
>      String str = "demo";
>      str = str + "start";
>      if(condition) {
>       str = str + "condition true";
>      } else{
>       str = str + "condition false";
>      }
>      str = str + "end";
>     }
> it produced jimp like this:
>     public void experiment(boolean)
>     {
>         Test this;
>         boolean condition;
>         java.lang.String str, temp$1, temp$3, temp$5;
>         java.lang.StringBuffer temp$0, temp$2, temp$4, temp$6;
>         this := @this;
>         condition := @parameter0;
>         str = "demo";
>         temp$0 = new java.lang.StringBuffer;
>         specialinvoke temp$0.<init>();
>         temp$0.append(str);
>         temp$0.append("start");
>         temp$1 = temp$0.toString();
>         if condition == 0 goto label0;
>         temp$2 = new java.lang.StringBuffer;
>         specialinvoke temp$2.<init>();
>         temp$2.append(temp$1);
>         temp$2.append("condition true");
>         temp$3 = temp$2.toString();
>         str = temp$3;
>         goto label1;
>      label0:
>         temp$4 = new java.lang.StringBuffer;
>         specialinvoke temp$4.<init>();
>         temp$4.append(temp$1);
>         temp$4.append("condition false");
>         temp$5 = temp$4.toString();
>         str = temp$5;
>      label1:
>         temp$6 = new java.lang.StringBuffer;
>         specialinvoke temp$6.<init>();
>         temp$6.append(str);
>         temp$6.append("end");
>         temp$6.toString();
>         return;
>     }
> It's obviously that string concatenation through "+" will be replaced a
> sequence of code like creating a new StringBuffer and utilizing its append
> to concatenate the pieces, in order to improve performance.
> Well, the problem was that when I try to find out any expression
> like" <java.lang.StringBuffer: java.lang.StringBuffer
> append(java.lang.String)>" by using the equals() of String. The result
> turned out none of the above expressions found while actually there were
> serveral.
> When I dubugged the program, I discovered that object actually was
> StringBuilder instead of StringBuffer, so the signature became
> <java.lang.StringBuilder: java.lang.StringBuilder
> append(java.lang.String)>.So by comparing the signature I could not find any
> matches.
> Any explanation or suggestion will be greatly appreciated.
> Kelvin
>
> _______________________________________________
> Soot-list mailing list
> Soot-list at sable.mcgill.ca
> http://mailman.cs.mcgill.ca/mailman/listinfo/soot-list
>
>


More information about the Soot-list mailing list