On Thu, Mar 16, 2000 at 01:28:40PM +0000, Phillip Lord wrote:
>       I can see no extra objects being created here with using the
> StringBuffer.append( String ) method. Unless you mean the "///" string
> literal. This will come from javas internal string cache, and
> therefore represents one extra object creation per VM invocation not
> per append invocation which is not really worth worrying about, and
> for which the code simplicity would make worth while.
> 

Don't worry you are not being dim.  First your assuming a well          
constructed VM when speaking of a string cache, and I've seen no        
evidence that sun or blackdown by extension has implemented such a      
beast.  But I won't quible over a one string object.  Although a better 
choice would be to use                                                  
String quote = "\"";
String escape = "\\";
That way you are assured that the string regardless of its creation
means, is only allocated once.

But lets examine these two methods
valueOf contains one if construct
getChars contains three if's then an arrayCopy
arrayCopy is native code so all bets are of on its efficiency, consindering
that code is probablly doing the same boundary checks we just performed

On the other hand
the append(char)
contains one if, and array index and an increment

so append(str) contains 6 if statements
to append(str)'s 1

But to avoid all this the code as presented calculates the number of 
escapes to be caluclated
char [] array = new char[length + escapeCount];
int index;
for (int i = 0; i < length; i++) {
// do switch here and just to insertion into array here
}


This way you could avoid all extraneous logic since we have already
ensured the length of our final string.

Brad

> 
> from StringBuffer.java
> 
> public synchronized StringBuffer append(String str) {
>       if (str == null) {
>           str = String.valueOf(str);
>       }
> 
>       int len = str.length();
>       int newcount = count + len;
>       if (newcount > value.length)
>           expandCapacity(newcount);
>       str.getChars(0, len, value, count);
>       count = newcount;
>       return this;
> }
> 
> public synchronized StringBuffer append(char str[]) {
>       int len = str.length;
>       int newcount = count + len;
>       if (newcount > value.length)
>           expandCapacity(newcount);
>       System.arraycopy(str, 0, value, count, len);
>       count = newcount;
>       return this;
> }
> 
> public synchronized StringBuffer append(char c) {
>         int newcount = count + 1;
>       if (newcount > value.length)
>           expandCapacity(newcount);
>       value[count++] = c;
>       return this;
> } 
> 
> 
> from String.java
> 
> public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
> 
>       [....Array index checking removed...]
>       System.arraycopy(value, offset + srcBegin, dst, dstBegin, 
>                        srcEnd - srcBegin);
> }

-- 
--- There are two kinds of knowledge, you either know the answer or
                        you know where to find it
                      -Kane, Johnson, and anonymous

Reply via email to