-5
String s1="abc";                  //line 1
String s2 = new String ("abc");   //line 2
String s3 = "abc";                //line 3
String s4 = s1+s2+s3;             //line 4

I need to know how many objects are created in total.(Heap and SCP area).

At line 1, one object "abc" in SCP

At line 2, one object "abc" in heap

At line 3, s3 points already existing object "abc" in SCP

Till this point there are 2 objects. I am not able to figure out exactly how many after line 4. Is there a way to find out how many objects are created in these cases using a java program?

Akash Kaundinya
  • 109
  • 1
  • 6

1 Answers1

0

I don't think you can check how many objects created using java code. After executing line 4 total 4 objects will be created.

  1. "abc" (Heap memory)
  2. "abc" (SCP)
  3. "abcabc" (SCP)
  4. "abcabcabc" (SCP).