<snip/>

> Some more insight or confusion. The byte code maybe similar in the sense
> that String uses "".concat() and
> StringBuffer uses new StringBuffer().append to do their individual
> concatenations but the way they are
> treated by the JVM is not the same. Of course not all JVM's are created
> equal but Strings are stored as constants thus you see the ldc opcode when
> creating Strings. Even though a String holds on to an internal
> character array as does a StringBuffer, a String creates a new String when
> it is concatentating another String to itself NOT modifing its internal
> character array. On the other hand a StringBuffer actually modifies its
> internal character array to represent the new String though this is
> accomplished by increasing the array size by creating a new char array.
> This is only part of the story since there is a difference between Strings
> stored in the so-called constants pool and new Strings created during
> runtime which do not go into the constants-pool automatically. Very good
> info concerning this on the web.

Okay firstly, please look at the pcode generated earlier in this thread.
It was generated by the moder compiler on jdk 1.4.0 the string addition
is indeed concatinated using StringBuffer.append()

    public String testStringBufferChained()
    {
        return (new StringBuffer().append("this ")
            .append(makeString("is "))
            .append("a ")
            .append(makeString("test"))).toString();
    }

    public String testStringAdd()
    {
        return
            "this "
            + makeString("is ")
            + "a "
            + makeString("test");
    }

becomes ...

Method java.lang.String testStringBufferChained()
   0 new #2 <Class java.lang.StringBuffer>
   3 dup
   4 invokespecial #3 <Method java.lang.StringBuffer()>
   7 ldc #4 <String "this ">
   9 invokevirtual #5 <Method java.lang.StringBuffer
append(java.lang.String)>
  12 aload_0
  13 ldc #6 <String "is ">
  15 invokespecial #7 <Method java.lang.String
makeString(java.lang.String)>
  18 invokevirtual #5 <Method java.lang.StringBuffer
append(java.lang.String)>
  21 ldc #8 <String "a ">
  23 invokevirtual #5 <Method java.lang.StringBuffer
append(java.lang.String)>
  26 aload_0
  27 ldc #9 <String "test">
  29 invokespecial #7 <Method java.lang.String
makeString(java.lang.String)>
  32 invokevirtual #5 <Method java.lang.StringBuffer
append(java.lang.String)>
  35 invokevirtual #10 <Method java.lang.String toString()>
  38 areturn

Method java.lang.String testStringAdd()
   0 new #2 <Class java.lang.StringBuffer>
   3 dup
   4 invokespecial #3 <Method java.lang.StringBuffer()>
   7 ldc #4 <String "this ">
   9 invokevirtual #5 <Method java.lang.StringBuffer
append(java.lang.String)>
  12 aload_0
  13 ldc #6 <String "is ">
  15 invokespecial #7 <Method java.lang.String
makeString(java.lang.String)>
  18 invokevirtual #5 <Method java.lang.StringBuffer
append(java.lang.String)>
  21 ldc #8 <String "a ">
  23 invokevirtual #5 <Method java.lang.StringBuffer
append(java.lang.String)>
  26 aload_0
  27 ldc #9 <String "test">
  29 invokespecial #7 <Method java.lang.String
makeString(java.lang.String)>
  32 invokevirtual #5 <Method java.lang.StringBuffer
append(java.lang.String)>
  35 invokevirtual #10 <Method java.lang.String toString()>
  38 areturn


So once again I say these are these are identical, the underlying VM has
no idea of any difference. I would though contend that the String
addition is easier to read.

-k.

-- 
If you don't test then your code is only a collection of bugs which 
apparently behave like a working program. 

Website: http://www.rocketred.com.au/blogs/kevin/


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, email: [EMAIL PROTECTED]

Reply via email to