My ans was 0 as there is no object created here and String literals are not garbage collected. But answer is not that. what would be the correct answer for this?
Aren't string literals stored in String Pool? I read that Strings in String Literal Pool are not garbage collected? Am I missing relavence to String Literal Pool some where here?
Naseem Khan
Ranch Hand
Joined: Apr 25, 2005
Posts: 809
posted
0
They are stored in String pool. But they are called String literals objects.
e.g.,
String str=new String("hello");
here if "hello" is not found in String pool, then Java will create two objects.
1. String object in non-pool memory 2. String literal object in String constant pool.
Whereas if String str="hello1";
then Only one String object is created in String constant pool.
regards
Jayes Herryl
Greenhorn
Joined: Jun 09, 2006
Posts: 7
posted
0
So does that mean only name can be garbage colleccted?
sri latha
Greenhorn
Joined: Mar 03, 2006
Posts: 27
posted
0
Hi i have one doubt on this question.i thought the answeris c.2 objects are eligible for garbage collection.object 'nick' is also eligible for garbage collection.becaure newname refer to 'jason'.'Freida' is garbage collected because it is null. can anyone explain wheather my guess was correct or not. preparing for scjp1.4
wise owen
Ranch Hand
Joined: Feb 02, 2006
Posts: 2023
posted
0
Please Check this thread. "Nick" String Object is in the String Literal Pool.
Pramila Chinguru
Ranch Hand
Joined: May 05, 2006
Posts: 54
posted
0
Thanks to wise and Naseem, but still I am not clear about this question. Here is my explanation about this qn.
String newName = "Nick"; Nick is created in heap,with a newName referencing it.Also reference is stored in String Literal Pool table. So it has two reference, one is with newName and the other is in String Literal Pool.
newName = "Jason"; //same as new String("Jason");
Jason Object is created and its only reference is new Name. Point to note, Nick has still one reference from string literal pool.
name = "Frieda"; //same as new String("Frieda");
Frieda Object is created and its only reference is name.
String newestName = name;
newestName also refers to Frieda Object. Hence two references to Frieda Object (name and newestName)
name = null;
name is made null, but still there is a reference to Frieda Object through newestName. hence "Frieda" is not eligible for GC. "Jason" is referenced by newName. Hence it is not eligible for GC. "Nick" is in String Literal Pool. That is there is a reference to Nick from a constant pool.(according to corey tip line).
Can some one point out where did I go wrong?
wise owen
Ranch Hand
Joined: Feb 02, 2006
Posts: 2023
posted
0
I will pick up the "a) 0" answer you picked up.
Bonny Sen
Greenhorn
Joined: May 09, 2006
Posts: 26
posted
0
Hi Pramila,
Your explanition sounds perfect. But here is how I feel thinds go:
The moment you do newName = "Jason"; then the String object "Nick" becomes like an orphan object having no variable referencing it. This object is simply LOST. Since now we do not have any refernce to "Nick", and so "Nick" object is eligible for garbage collection.
I am not sure whether Strings in String Literal Pool are garbage collected or not. I have to confirm on this. I belive they are GC. If not then think about a large probram where a lot of String objects are created. If GC was not possible then the String Literal Pool would have a huge collection of String objects, resulting in high consumption of memory , and the very first idea of having GC goes haywire.
So I believe that there will be 1 object eligible for GC. Please correct me if I am wrong anywhere in my explanation.
With Love, Bonny
"Life is like an ice-cream, Enjoy it before it melts..."
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.