• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

garbage collection

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How many objects are eligible for garbage collection after executing line 7?

public class Tester {

public static void main(String[] args) {

Integer x = new Integer(3000);
Integer y = new Integer(4000);
Integer z = new Integer(5000);

Object a = x;
x = y;
y = z;
z = null; //line 7
}
}

1) 0
2) 1
3) 2
4) 3

i think it should be 0 ie first option bt its right option was 1 how it will be please someone answer it.
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's zero yeah. But without the Object a=x, it's one. Could we have your source?
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
0 objects eligible for GC
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think, the Answer is 1. Only 'z' will be eligible for GC !!

x, y, & a links to same number, while only z is linking to null at the end.
So only z will be eligible for GC !!

Just draw a diagram, that way it will be very easy to understand !!

Thanks,
Ved
 
Rohit Ramachandran
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
z is not an object. z is a reference varibale -_-;
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
santosh prabhat, please UseCodeTags when you post a code snaps! Use the EDIT button to edit it and insert the code tags!

There are no objects eligible for GC. And, further, GC isn't for reference variables. It's for objects.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic