-1

Suppose following is the piece of code, how many string objects are created and where(StringPool or heap memory) Mention the references which are pointing to the appropriate objects.

String s1 = "abc";
String s2 = s1;
String s3 = new String("abc");
String s4 = s3;
Sharath
  • 407
  • 5
  • 16

5 Answers5

2

Total 2 created elements (except their reference):

  • 1 String in pool memory => "abc"
  • 1 String object on the Heap => new String("abc"); ("abc" still referring to the first "abc" in the pool).

Others are just referencing existing ones.

No more ;)

Mik378
  • 21,881
  • 15
  • 82
  • 180
1
String s1 = "abc";

variable s1 will refer to the string literal hi that is referenced from String constant pool and if we talk about

 String s2 = s1;

they both are referring to the same value stored at String pool.

String s3 = new String("abc");

This will create a new String at runtime.

In first case ,all the string literals are created when class is loaded in JVM In seconds case, string objects are created when new String() is executed.

String s4 = s3;

they both are referring to the same object stored at heap.

You can find a good tutorial about string constant pool at following link

http://www.thejavageek.com/2013/06/19/the-string-constant-pool/

Kshitij Kulshrestha
  • 2,032
  • 1
  • 20
  • 27
1

2 Objects and 4 References

String s1 = "abc";
// An object with value"abc" is created; s1 is a reference variable pointing to the newly created object

String s2 = s1;
//Only reference variable s2 is created; It will point to the existing object

String s3 = new String("abc");
//A NEW object is created irrespective of the value. Thumb rule is whenever there is a new operator an "Object" will be created. Reference variable S3 is made to point the newly created object

String s4 = s3;
//Only reference variable s4 is created, and the explanation is similar to ref variable s2
trompa
  • 1,967
  • 1
  • 18
  • 26
nachiappanpl
  • 693
  • 1
  • 5
  • 8
0

I think there are two object created s1 and s3 which goes in heap memory. s2 and s4 are references.

Amit Jain
  • 143
  • 8
  • I'm pretty sure about s1 created in string pool, since we using the literals here. s2 will be the reference to the same object created in string pool. s3 will be in heap memory, I believe and s4 is what I'm not sure about pointing to the object created in heap memory or not? – Sharath Oct 28 '14 at 11:28
  • THanks Sharath,...i think s4 will not go in heap memory as it is just a ref pointing to s3. – Amit Jain Oct 28 '14 at 11:32
0

Two String objects will be created. "abc" will be created in pool memory . Actually new String("abc") will create two String object but as "abc" is already in pool it will not be created again. so the statement new String("abc") will create 1 object and it will point to string literal "abc" .

Nilesh
  • 133
  • 3
  • 10
  • `"abc"` will create a string pool object at compile time/class load time. `new String(...)` will create one string. – user207421 Mar 23 '17 at 05:47