• 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

Question on gc

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How many objects are avialble for GC

class CardBoard {
Short story = 5;
CardBoard go(CardBoard cb) {
cb = null;
return cb;
public static void main(String[] args) {
CardBoard c1 = new CardBoard();
CardBoard c2 = new CardBoard();
CardBoard c3 = c1.go(c2);
c1 = null;
// do Stuff
}
}

i think 2 cause c1 is null and c3 is being returned as null so remaining two objects are C2 and short but K&B books says following


A. 0
B. 1
C. 2
D. Compilation fails.
E. It is not possible to know.
F. An exception is thrown at runtime.

C is correct. Only one CardBoard object (c1) is eligible, but it has an associated Short
wrapper object that is also eligible.

I mean answer is right but object is different why C1

[ September 30, 2008: Message edited by: mukki pandey ]
[ September 30, 2008: Message edited by: mukki pandey ]
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the question here?
 
Ranch Hand
Posts: 206
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I cant understand your question.
 
mukki pandey
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How many objects are avialble for GC

according to me C2 and short but k&b says C1 and short how C1 is avialable for GC since its already assigned to null
 
mukki pandey
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can anyone reply please
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Objects created in the example are 4 i.e. one referenced by c1 and short in c1 & the other one by c2 and short in c2.
c3 is just a refernce variable having null value.
As c1 is assigned null value, only the object which is referenced by c1 and short in c1 are eligible for GC. c2 is still referring to the object which was created in line 2. So it is not eligiible for GC.

Remember copy of variable c2 is passed to the method c2!

one thing : you have missed closing brace for the method go()!
[ September 30, 2008: Message edited by: M SRILATHA ]
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also check on the Errata for the book. The Short is created by autoboxing, and based on the value (and due to the cache), it isn't eligible for GC. In the corrected version, the short has a larger value.

Henry
 
mukki pandey
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks :-) i got it
 
reply
    Bookmark Topic Watch Topic
  • New Topic