简体   繁体   中英

Where is the String literal which was passed in the constructor, stored when we create a new String object

In Java when we write

String S1 = "TestString";
String S2 = "TestString";

and then compare with if(S1==S2) , we get true as the boolean result. The explanation for the same being that the String constants are created in the String Pool and hence it is the same string constant that is being referred here by both S1 and S2. Also, if we write something like

String S1 = new String("TestString");

String S2 = new String("TestString");

and then do the comparison with if(S1==S2) , we get false. The reason being that the references of S1 and S2 are different since the strings literals are created in the heap.

My Question is that where was the String literal "TestString" which was passed in the constructor created? Is it same as a String literal/constant? and hence should be created in the Pool as was in the case 1 ? if it is then when we write something like after the above two statements

String S3 = "TestString";

this should not create a new String literal and comparing if(S1==S3) should give me true but it gives false.

So i am not able to figure out where and when is this string literal passed in the constructor getting created.

Any help would be greatly appreciated. Thanks

My Question is that where was the String literal "TestString" which was passed in the constructor created? Is it same as a String literal/constant? and hence should be created in the Pool as was in the case 1 ?

Correct, the constant string passed to the constructor invocation new String("TestString") is stored in the string pool, just like in the statement String S1 = "TestString" .

String S1 = new String("TestString");

String S2 = new String("TestString");

String S3 = "TestString";

In this case S1==S3 gives false because S3 refers the string literal that was created by the argument of the constructor used for S1 , whereas S1 is a different string (because it is created with the constructor).

if(S1==S2), we get false.

That's is because you have three different Strings, S1, S2 and the string literal. S1 and S2 are copies of the String object and have different references. Thus none of them are == one another.

My Question is that where was the String literal "TestString" which was passed in the constructor created?

It was created on the heap like all other objects (Since Java 6) It was also added to the String intern() pool so any other String literal which as the same would be the same object.

Is it same as a String literal/constant?

All the String literals with the same contents will be the same object.

String S3 = "TestString"; this should not create a new String literal

Correct. The String literal has already been created.

comparing if(S1==S3) should give me true

I have no idea why. S1 is a copy of the string literal, not the string literal itself.

but it gives false.

as expected.

So i am not able to figure out where and when is this string literal passed in the constructor getting created.

Before Java 7, String literals were created when the class was loaded, in Java 7+ it is created when it is first used.

if both S1 and S3 share the same string literal/constant

Up to Java 6, they shared the same underlying char[]. From Java 7, this is not true. The String objects have always been and will always be different.

S1 has the reference ie it stores the memory address of the place where the constant "TestString" was actually stored

S1 is a copy so it store a reference of a copy of the string literal.

I still have one question though that when we compare S1 and S3 reference wise then why are they not equal(they must be holding the same memory addresses)

They have different addresses which is why == gives false.

S1 and S2 are both on the stack and they contain the address values of the respective objects which they refer to from the Heap. So S1 and S2 itself will occupy different locations(addresses).

Notionally S1 and S2 have different location and point to different locations. However when the code is turned in machine code to actually run it can be optimised such that variables are in registers, not on the stack and any variable which is not used is discarded. ie it could be that no objects ever exist.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM