Ankur Kapoor wrote:
2. Now lets take a scenario where you would have used new String() pre String literal created in a POOL
String myString = new String("fun"); // this shall create a New Object in HEAP but not in POOL . [OBJECT CREATED] hence creating only single Object that is in HEAP
3. Now let's use intern() function of String to make the String Objects created using new operator available in the POOL
String myString = new String("fun").intern() // this shall create a New Object in HEAP and also place it in the POOL . [OBJECT CREATED]
I am not getting above statements because it contradicats with article of Strings in this forum as well as Book by Kathy Sierra. OR may be the reason is I am misunderstood somewhere and not getting the actual thing. So, here I am clearing all things and tell me where I am wrong with valid reason.
I would like to start with statements of Kathy Sierra's Book on page no. 434.
Kathy Sierra wrote:
In this case, because we used the new keyword, Java will create a new String object in normal (nonpool) memory, and s will refer to it. In addition, literal "abc" will be placed in the pool.
Now, I am concluding following things. Tell me where I am wrong.
Consider each and every of the following scenarios is independent and has no relationship with previous scenario.
1)
Only one object of String literal "abc" will be created here and it will be referred from String Literal Pool Table as well as it will be referred by Reference variable s.
2)
Here due to new keyword, one String object will be created in heap. Now we are passing String literal "def" to the String constructor. So, again one object of String literal "def" will be created and it will be referred from Strin Literal Pool Table just like in previous case but as we used new operator here, one extra object is created containg same literal "def" but it will not be reference from String Literal Pool and will be eligible for Garabage Collection. So, on the whole two objects are created in this case.
3)
now I want to know is how many object will be created in this case just and only because of line no.2?
After the end of first line, one object will be created as I mentioned in case 1.
Because of 2nd line, definately one object will be created containing String literal "ghi" due to new keyword which will not be referred from Strin Literal Pool Table. But I think here unlike in case 2, one more object containg String literal "ghi" will not be created which we are passing in String constructor because it's alreday available after line 1.
So, answer is only one new object is created just and only because of line no.2
4)
anybody can argue after reading my above statements that what is the use of intern() method when String is definately going to be referred from String Literal Table whether we use new keyword or not. I have following reasons for this:
i> If we have passed String literal while creating String using new operator, then there will be two objects in Heap containg same strings.One will be referred by reference variable and one will be referred from String Literal Pool. Now, if we apply intern() method on reference variable, then reference variable will also refer to the object which is being referred from String Literal Pool Table and another object will be eligible for garbage collection
ii> Another case where intern() method can be really helpful is when we do not pass String literal in String constructor and make String using array of characters or using any other valid arguments. At this time, as we have not passed String literal in an argument, only one object will be created which will not be permanent but after intern() method, it will be permanent as now it will be referred from String literal Pool.
Please tell me whereever I am wrong in any of above statements.
Regards