Last updated: 4/9/02; 4:29:09 PM

The Desktop Fishbowl
tail -f /dev/mind > blog

StringBuffer handling

Notes

Tests performed on an OS X 10.2 system running the supplied 1.3.1 compiler, which is basically Sun's compiler. The compiled bytecodes were identical on my Linux box running 1.4.0

The Class

public class Test {
        private String a = "alpha";
        private String b = Beta;
        private String c = "gamma";
 
        public String test1() {
                return a + b + c;
        }
 
        public String test2() {
                StringBuffer s = new StringBuffer(a);
                s.append(b);
                s.append(c);
                return s.toString();
        }
}

Test1 decompiled:

Method java.lang.String >test1()
   0 new #8 <Class java.lang.StringBuffer>
   3 dup
   4 invokespecial #9 <Method java.lang.StringBuffer()>
   7 aload_0
   8 getfield #3 <Field java.lang.String a>
  11 invokevirtual #10 <Method java.lang.StringBuffer append(java.lang.String)>
  14 aload_0
  15 getfield #5 <Field java.lang.String b>
  18 invokevirtual #10 <Method java.lang.StringBuffer append(java.lang.String)>
  21 aload_0
  22 getfield #7 <Field java.lang.String c>
  25 invokevirtual #10 <Method java.lang.StringBuffer append(java.lang.String)>
  28 invokevirtual #11 <Method java.lang.String toString()>
  31 areturn

Test2 decompiled

Method java.lang.String test2()
   0 new #8 <Class java.lang.StringBuffer>
   3 dup
   4 aload_0
   5 getfield #3 <Field java.lang.String a>
   8 invokespecial #12 <Method java.lang.StringBuffer(java.lang.String)>
  11 astore_1
  12 aload_1
  13 aload_0
  14 getfield #5 <Field java.lang.String b>
  17 invokevirtual #10 <Method java.lang.StringBuffer append(java.lang.String)>
  20 pop
  21 aload_1
  22 aload_0
  23 getfield #7 <Field java.lang.String c>
  26 invokevirtual #10 <Method java.lang.StringBuffer append(java.lang.String)>
  29 pop
  30 aload_1
  31 invokevirtual #11 <Method java.lang.String toString()>
  34 areturn





Copyright 2002 © Charles Miller