javap reports incorrectly aStringBuffer.toString()

javap reports that the toString method of the class String is invoked when it should be the toString method from the class StringBuffer.

Given this program

public class Test2 {

Test2() {

String a ="a", b ="b";

System.out.println(a + b);

}

}

the javap output is

Method Test2()

0 aload_0

1 invokespecial #1 <Method java.lang.Object()>

4 ldc #2 <String "a">

6 astore_1

7 ldc #3 <String "b">

9 astore_2

10 getstatic #4 <Field java.io.PrintStream out>

13 new #5 <Class java.lang.StringBuffer>

16 dup

17 invokespecial #6 <Method java.lang.StringBuffer()>

20 aload_1

21 invokevirtual #7 <Method java.lang.StringBuffer append(java.lang.String)>

24 aload_2

25 invokevirtual #7 <Method java.lang.StringBuffer append(java.lang.String)>

28 invokevirtual #8 <Method java.lang.String toString()>

31 invokevirtual #9 <Method void println(java.lang.String)>

34 return

In line 28 there is a call of the toString method in class String on a StringBuffer object. While the following shows that the constant pool entry number 8 is a symbolic reference to the toString method declared in StringBuffer class:

--

10atag(8)CONSTANT_Methodref

00class_indexjava/lang/StringBuffer

55

00name_and_type_indextoString()Ljava/lang/String;

2519

--

In http://usuarios.tripod.es/JoseBotella a class file parser is freely available for this output to be checked

[1621 byte] By [Botella] at [2007-9-26 6:34:37]
# 1
I saw this exact question on another forum, and it had an answer there.
DrClap at 2007-7-1 15:47:21 > top of Java-index,Java HotSpot Virtual Machine,Specifications...