This is the output that I got: abababcd
Tested on Win2k - Java 5 version.
First of all we must remember that the StringBuffer ultimately uses a Char array to store the contents of the string. Since we're using the same buffer object to do the insert, we're modifying the contents of the underlying array inturn. This is what is actually happening...
StringBuffer sb = new StringBuffer("abcd");
The above line creates a Char array containing {'a','b','c','d'}
sb.insert(2,sb);
Now are trying to insert at index two of the above created array, the point to note is that both the source & destination is the same char array. Initially the array len=4, during copy this what happens..
element 1 --> {'a','b','a','c','d'}
element 2 --> {'a','b','a','b','c','d'}
element 3 --> {'a','b','a','b','a','c','d'}
element 4 --> {'a','b','a','b','a','b','c','d'}
[ September 16, 2005: Message edited by: Jimmy Thomas ]