Quoting this page:
How many objects will be eligible for GC here?
String s = "hello"; Character ch1 = 1; Character ch2 = 1; ch1 = null; s = null;
I believe the answer is 1.
I would like to understand how it works in Java 6.
My understanding at the moment:
String is going to the pool. Later, there is no reference to it. So, according to this answer (I don't understand that part about classloader, can you clarify it?), String pool will most likely not be garbage collected...
Ok, Characters. There is this optimization in Java that will cause that ch1
and ch2
will point the same object. So, this small Characters are also going to some pool. But, despite ch1
is null
, we still can access 1
with ch2
reference.
So, my answer at the moment would be 0.
Am I right in every step? If not, please correct me. Could you please provide an explanation how does it work exactly?