Results 1 to 10 of 10
Thread: Garbage Collector Doubt
- 05-03-2010, 05:16 PM #1
Member
- Join Date
- May 2010
- Posts
- 3
- Rep Power
- 0
Garbage Collector Doubt
Hi,
I'm studying to become java programmer certification exam, and I faced this 'self test question' (taken from the SCJP Sun Certified Programmer for java 6-0071591060 book, page No. 277):
Given:
/*-----------------------------------------------------------------*/
class CardBoard {
Short story = 200;
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
}
}
/*------------------------------------------------------------------*/
When // doStuff is reached, how many objects are eligible for the Garbage Collector?
...
The answer the book gives me is : Only one CardBoard (object (c1)) is eligible and its Short wrapper object.
So, my worry is:
What happend with the c2 and c3 objects.
I can even use the object referenced by the c2 object after being used (= null) in the go method of c1. How can this happend?
Could somebody help me out, explaining me what is really occurring here.
Sorry for my english vocabulary.
- 05-03-2010, 06:16 PM #2
- 05-03-2010, 06:32 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,604
- Blog Entries
- 7
- Rep Power
- 17
- 05-03-2010, 07:29 PM #4
Member
- Join Date
- May 2010
- Posts
- 3
- Rep Power
- 0
Now I understand the context of java passing values.
As the book says : "And that means, pass-by-copy-of the-variable!"
Page 214. (Does Java Use Pass-By-Value Semantics?)
I thought that when the go() method was executed it was going to make a null reference of the reference passed to it.
Thanks a lot JosAH.
- 05-03-2010, 07:53 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,604
- Blog Entries
- 7
- Rep Power
- 17
- 05-03-2010, 09:30 PM #6
what is not true? i never affirmed that java has a pass by reference. but look at the code: from the main method you call CardBoard c3 = c1.go(c2); and the return value is null and the value null is set in the main method and not in a subroutine. so also c3 is null and eligible.Last edited by j2me64; 05-03-2010 at 09:36 PM.
- 05-03-2010, 10:10 PM #7
Member
- Join Date
- May 2010
- Posts
- 3
- Rep Power
- 0
c3 is not eligible because there has never existed an 'object' for the 'reference' c3 as c1 have.
CardBoard c1 = new CardBoard(); (An object in memory)
CardBoard c2 = new CardBoard(); (Another object in memory)
CardBoard c3 = c1.go(c2); (Java has never created an object for c3 at this point)
Just a reference to 'nothing' (no eligible object).
You can give it a try; c2 is still alive after the " // do Stuff" section.
c3 cannot be accessed because it has no a reference to anything.
- 05-03-2010, 10:36 PM #8
the possible answers are
A. 0
B. 1
C. 2
D. Compilation fails
E. It is not possible to know
F. An exception is thrown at runtime.
the right answer has nothing to do with pass-by-value. i cite the scjp-book p. 277
now it should be clear from where the second object come from.C is correct. Only one CardBoard object (c1) is eligible, but it has an associated Short wrapper object that is also eligible.
- 05-04-2010, 07:36 AM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,604
- Blog Entries
- 7
- Rep Power
- 17
- 05-04-2010, 11:26 AM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Similar Threads
-
When does the Garbage Collector pick up an object?
By nolsen01 in forum New To JavaReplies: 5Last Post: 02-27-2010, 03:32 PM -
Q about Garbage Collector
By m00nchile in forum New To JavaReplies: 4Last Post: 02-05-2010, 05:57 AM -
Garbage Collector and finalize()
By arefeh in forum New To JavaReplies: 5Last Post: 01-09-2010, 09:04 PM -
Garbage collector and its impacts
By RadhaJayalakshmi in forum Advanced JavaReplies: 1Last Post: 07-23-2008, 11:56 AM -
Interacting with the Java Garbage Collector
By Java Tip in forum Java TipReplies: 0Last Post: 03-28-2008, 08:04 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks