• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Garbage collection question

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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[1];
int[] scores = new int[1];
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[1] and int[1]) so another two objects. I don't see why the right answer must be two...

Does anyone have a clue?

Best regards,
Paul
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79180
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic