Originally posted by vatsalya rao: ...If I sound ridiculuous in my doubts Iam really sorry.
No issues Vatsalya. That's why this forum is for and we are all learning by discussing only
Let me take a moment to clarify.
Kindly make use of Code Tags whenever you paste the java code so that it will look uncluttered.
Before we discuss about this specific question, lets see a small example.
As such, we do have 2 reference variables named s1 and s2 of type java.lang.String. There are two string constants whose value equals "Sample" and "String".
As the term indicates "String Constants" are being evaluated at the compile time and NOT at the runtime. The String constants are created when the class is loaded and they do reside in a special area called String Literal Pool and hence they do not belong to the traditional Garbage Collectible Heap!
If you are clear with this, there is one more thing we need to see here.
The second line in teh above example creates a brand new String because of the new operator, as the JVM is instructed to oblige you to do so until and unless you intern the string by invoking the intern() method on the particular string reference.
As a conclusion, here only ONE string object is created as a result of executing the line 2 and because of the new operator. As such, here the newly created String object refers to the String "String" which is already created and referenced from the String Literal Pool.
One special note is that the constants in the String Literal Pool are never eligible for Garbage Collection as they don't belong to the Garbage Collectible Heap. They always have a reference to them until the JVM shuts down or the java.lang.String class is unloaded.
Now, lets look at your example.
We can infer a few things from the code as per our sample program with 2 lines earlier.
During the class loading operations, the following String constants are created and put in String Literal Pool. "Nick", "Jason", and "Frieda".
Please keep in mind that, they are NOT the objects of Garbage Collectible Heap, instead those are the constants part of String Literal Pool.
As such,there are NO new garbage collectible objects created at all. all what you deal is the aforesaid string constants.
So setting the string reference variable here will have no effect.
The available options are:
Since, no object is getting created in the Garbage Collectible Heap, no object is eligible for GC.