Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to understand whether a new String object has been created

Tags:

java

string

oop

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".

  • I cannot understand (nor are the people helping me) where did i get the concept wrong?
  • Does `ldc #index` means a object is created and linked with a reference from the pool, but it does-not imply that a "new" object has been created?
like image 334
shortCircuit Avatar asked Jan 23 '14 09:01

shortCircuit


People also ask

When we create a String object is created in?

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”.

How do you check if a string is an object?

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!

How many objects are created when we use new string?

The answer is: 2 String objects are created.

Does new string create object in string pool?

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.


2 Answers

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.

like image 100
Brian Roach Avatar answered Oct 02 '22 12:10

Brian Roach


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.

like image 29
Ivey Avatar answered Oct 02 '22 13:10

Ivey