Hi, I have this doubt regarding Garbage Collection.
Lets say we have class called Sample.
Lets Imagine the reference pointing to becomes null
I understand 2 objects will be eligible for garbage collection. One is the Class Sample and one is the Wrapper class within the Class Sample. int "a" will not be eligible right, since its a primitive type. Can someone please confirm if my understanding is correct.
Source of this question : I created this question myself. Apologies if this question has been asked before..
You might want to say '2 instances' somehow, instead of 'Class Sample.' As I understand it, classes themselves never become eligible for garbage collection - just instances of those classes.
Also - if you want to get around the '-128 to 128 Wrapper classes are reused' issue, change your "Integer x = 20" to "Integer x = new Integer(20)" instead. This guarantees you create a new object, instead of reusing low-valued wrapper objects.
Ankit Garg wrote:Deepak is right, since the value 20 is in the range of -128 to 127, so the Integer object created by autoboxing will be from the integer constant pool hence only one object will be eligible for GC...
Even though, after the object s is destroyed, is there a reference to that wrapper Integer object? Or
Integer constant pool object are not eligible for GC, because it can be reused by other object(It's the intention of the integer constant pool)?
And, is it same for String literal pool(even though it's not in the exam)?. Thanks in Advanced!
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|