I am confused by the answers and the results that I get when compiling a 3-line program. Here is the code along with its opcodes: http://pastebin.com/B1xxAjcp If I'm not completely mistaken, it’s obvious that
String s="abcd"; String s1=new String("efgh"); s.concat("ijkl");
these lines correspond to these opcodes:
1: istore_1 2: ldc #2 // String abcd 4: astore_2 5: new #3 // class java/lang/String 8: dup 9: ldc #4 // String efgh 11: invokespecial #5 // Method java/lang/String."<init> ":(Ljava/lang/String;)V 14: astore_3 15: aload_2 16: ldc #6 // String ijkl 18: invokevirtual #7 // Method java/lang/String.concat: (Ljava/lang/String;)Ljava/lang/String;
Thus, to my understanding, ldc #index means that instead of creating a new object, a link to the constant literal pool is created and pushes it onto the stack.
A new object is created, the new and dup commands occur before ldc #index . But in this question, How many String .. objects? , the second answer says that ldc #index implies that the String object was created. The explanation is as follows:
public static void main(java.lang.String[]); Code: 0: ldc #2
As you see, there is only one String object, which contains "ObjectOneObjectTwo".
- I can’t understand (and people don’t help me), where did I misunderstand the concept?
- Does “LdC # index” mean that the object was created and linked to from the pool, but it does not mean that the “new” object was created?
source share