I've a question about garbage collection, this is a question from a test-exam. The exam question is how many object are eligible for GC after the last line of code, the code is as follows:
String balls = new String;
int scores = new int;
balls = null;
scores = null;
The right answer must be two but to me it seems that the answer is four: there are two objects with a reference to the array (balls and scores) and there are two arrays with one element each (String and int) so another two objects. I don't see why the right answer must be two...
What it looks like to me is that ball = null; and scores = null; aren't separate from the arrays. It looks like it is declaring that the arrays are empty, instead of declaring separate variables. balls = null; and scores = null; don't have a data type attached to them. Otherwise that's the only way I could see the answer being 2.
Please always tell us where such questions come from to avoid copyright problems.
What was the answer given? 2?
You are getting confused about the difference between a reference and an object. I can see two objects, each being an array. Each of the two references you wrote points to one object, each an array.
The code shown doesn't enter any objects into the String, so its solitary element is a reference pointing to null. So there is no String object to delete. The solitary element in the int is 0 as a default, but that isn't an object.
A wop bop a lu bob a womp bam boom. Tutti frutti ad:
Programmatically Create PDF Using Free Spire.PDF with Java