I am confused by the answers and the results i am getting on compiling a 3 line program. Here is the code along with its opcodes: http://pastebin.com/B1xxAjcp If i am not totally wrong, its evident that
String s="abcd";
String s1=new String("efgh");
s.concat("ijkl");
these lines corresponds 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;
So to my understanding ldc #index
means that instead of creating a new object, creates a reference to the constant literal pool , and pushes it to stack.
A new object is created, new
and dup
commands occur before the ldc #index
. But in this question How many String object..? , the second answer says that ldc #index
implies that String object has been created,The explanation goes like this:
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".
There are two ways to create a String object: By string literal : Java String literal is created by using double quotes. For Example: String s=“Welcome”; By new keyword : Java String is created by using a keyword “new”.
Use the typeof operator to check if a variable is a string, e.g. if (typeof variable === 'string') . If the typeof operator returns "string" , then the variable is a string. In all other cases the variable isn't a string. Copied!
The answer is: 2 String objects are created.
Each time a string literal is created, the JVM checks the string literal pool first. If the string already exists in the string pool, a reference to the pooled instance returns. If the string does not exist in the pool, a new String object initializes and is placed in the pool.
String s="abcd";
String from literal "abcd" is created in String pool.
String s1=new String("efgh");
String from literal "efgh" is created in String pool. New non-interned String created, contents of String in pool copied to it
s.concat("ijkl");
String from literal "ijkl" is created in string pool. New non-interned String created, contents of two interned Strings copied into it.
This created 3 instances of String
in the pool, and 2 non-interned (not in the pool) instances of String
.
Edit to add: The lcd
bytecode op is pushing the reference (value) of a String
in the pool onto the stack.
In the first line, a new string "abcd" is created in the string pool. String object "s" is given a reference to this string. In the second line, another string "efgh" is created in string pool and a new object of type String "s1" is created, and it is given reference to the new string created in the pool. In the third line, another string "ijkl" is created in the string pool, after the concat() operation, another new string "abcdijkl" is created and now String object "s" refers to this newly created string. So, 3 objects in the string pool, and 2 on the heap.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With