my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on gc Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on gc" Watch "Question on gc" New topic
Author

Question on gc

mukki pandey
Ranch Hand

Joined: Sep 22, 2008
Posts: 58
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 ]
Ali Khalfan
Ranch Hand

Joined: Nov 03, 2007
Posts: 126
what is the question here?
chander shivdasani
Ranch Hand

Joined: Oct 09, 2007
Posts: 206

I cant understand your question.


Enjoy, Chander
SCJP 5, Oracle Certified PL/SQL Developer
mukki pandey
Ranch Hand

Joined: Sep 22, 2008
Posts: 58
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

Joined: Sep 22, 2008
Posts: 58
can anyone reply please
M Srilatha
Ranch Hand

Joined: Aug 27, 2008
Posts: 137
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 ]

Thanks,<br />Srilatha M
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
mukki pandey
Ranch Hand

Joined: Sep 22, 2008
Posts: 58
thanks :-) i got it
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Question on gc
 
Similar Threads
Garbage collector
Garbage Collection Doubt
K&B ch-3 Ques-2
help scjp5.0 question
K&B : Garbage Collection Question..doubt!!