The bytecode for:
StringBuffer name = new StringBuffer("Joe");
is located between locations 0 and 9. When these bytecodes are executed, they create oneStringBuffer objects at position 0 and one String object at position 4.
We will now examine the bytecode produced to construct the String parameter for the println method:
name.append(" is my name.").toString());
The bytecode that is executed for this statement is between addresses 13 and 19.
A new String is created at position 14 for the String constant (thus far, three objects have been created). This String is passed as a parameter to the append method at position 16. Another String object is created at position 19 via the toString method. Therefore, only four objects have been created with the above code.
0 new #53 dup 4 ldc #21 6 invokespecial #7 9 astore_1 10 getstatic #8 13 aload_1 14 ldc #25 16 invokevirtual #24 19 invokevirtual #26 22 invokevirtual #10