0

I did not Understand about concept of object creation String polling and heap

    String s1 = new String("text");
    String s2 = new String("text");
    String s3 = new String ("text1");
    String s4 ="text";
    String s5 = s4;
    String s6 = new String();
    s6 =s1; 

Getting debugger in Value tab

enter image description here

That Means it create 5 object when it will line no 6 enter image description here

But it will execute total object 4

And when I written program this way

     new String("text");
     new String("text");
     new String ("text");

enter image description here

it will create on heap but string polling it will created or not

2 Answers2

0

This is because Java has a special pool for String objects. Its called a String pool. There is a full discussion available where you can read all about it.

What is the Java string pool and how is "s" different from new String("s")?

Community
  • 1
  • 1
legopiraat
  • 166
  • 1
  • 11
-1

@Saurabh

when you wrote this,

new String("text");
new String("text");
new String ("text");

in affect of it it will create, Total 4-Object created, 3-Object of String on Heap and one "text" Object of String inside-pool.

Also, Be aware that it will become Eligible for Garbage-Collection too. Because there were not any live thread exist,if we consider to not refer by any-one else then.

Vishal Gajera
  • 4,137
  • 5
  • 28
  • 55
  • @vishal and if it is refrence some then how many object created same 4 –  Oct 12 '15 at 08:41
  • @SaurabhKumarSrivastava, it's depend out of 4, how many refer and how many not? Also need to check object directly refer by someone else or indirectly. so unless and untill without checking whole boundry, I can not predict how many recently out of 4 eligible. – Vishal Gajera Oct 12 '15 at 08:44