Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Garbage collection question  RSS feed

Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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,
Posts: 5
  • Mark post as helpful
  • send pies
  • 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.
Posts: 63853
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
Catch Ernie! Catch the egg! And catch this tiny ad too:
global solutions you can do at home or in your backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!