How to understand if a new String object has been created

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 // String ObjectOneObjectTwo 2: astore_1 3: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream; 6: aload_1 7: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 10: return } 

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?
+6
source share
3 answers
 String s="abcd"; 

A string from the literal "abcd" is created in the string pool.

 String s1=new String("efgh"); 

A string from the literal "efgh" is created in the string pool. A new uninterrupted string has been created, the contents of the String string copied to it

 s.concat("ijkl"); 

A string from the ijkl literal is created in the string pool. A new uninterrupted line has been created, the contents of two internal lines copied to it.

This created 3 String instances in the pool and 2 uninterrupted (non-pool) String instances.

Edit to add: The lcd bytcode byte operator calls the String link (value) in the pool onto the stack.

+5
source

The first line in the row pool creates a new line "abcd". The string object "s" gets a reference to this string. The second line creates the string "efgh" in the string pool and creates a new object of type String "s1" and gives it a link to the new string created in the pool. On the third line, another line “ijkl” is created in the row pool, after the concat () operation, another new line “abcdijkl” is created, and now the String “s” object refers to this newly created line. So, 3 objects in the row pool and 2 in the heap.

+2
source

I think the real thing is this:

  String s1=new String("efgh"); //this will create TWO objects : one in the pool and one in the heap. 
+1
source

All Articles