Results 1 to 10 of 10
  1. #1
    CristOm is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default 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.

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by CristOm View Post
    Could somebody help me out, explaining me what is really occurring here.
    the right answer is C. 2, that means that 2 objects are eligible by the gc. c3 has a null reference and also c1 so this 2 objects are eligible.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,538
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by j2me64 View Post
    the right answer is C. 2, that means that 2 objects are eligible by the gc. c3 has a null reference and also c1 so this 2 objects are eligible.
    That is not true; Java has pass by value so setting a parameter to null doesn't affect any original object passed in. c1 is set to null in the last statement so it's a prey for the garbage collector (including its Short member object).

    kind regards,

    Jos

  4. #4
    CristOm is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default

    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.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,538
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by CristOm View Post
    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.
    You're welcome of course; you ask one thing and learn something else, that's how a bit of research works ;-)

    kind regards,

    Jos

  6. #6
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by JosAH View Post
    That is not true; Java has pass by value so setting a parameter to null doesn't affect any original object passed in.

    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.

  7. #7
    CristOm is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by j2me64 View Post
    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.
    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.

  8. #8
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    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

    C is correct. Only one CardBoard object (c1) is eligible, but it has an associated Short wrapper object that is also eligible.
    now it should be clear from where the second object come from.

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,538
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by j2me64 View Post
    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.
    Your reply #6 suggested that the object referred to by c3 could also be garbage collected; c3 doesn't and didn't point to an object. It is true that only c1 (and its member Short object) can be garbage collected. Your wordings possibly confused me.

    kind regards,

    Jos

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default

    Quote Originally Posted by j2me64 View Post
    c3 has a null reference and also c1 so this 2 objects are eligible.
    Confused you?
    He's clearly saying c3 is eligible for collection...

    Yes, the answer is two, but not for the reasons stated in that quote.

Similar Threads

  1. When does the Garbage Collector pick up an object?
    By nolsen01 in forum New To Java
    Replies: 5
    Last Post: 02-27-2010, 03:32 PM
  2. Q about Garbage Collector
    By m00nchile in forum New To Java
    Replies: 4
    Last Post: 02-05-2010, 05:57 AM
  3. Garbage Collector and finalize()
    By arefeh in forum New To Java
    Replies: 5
    Last Post: 01-09-2010, 09:04 PM
  4. Garbage collector and its impacts
    By RadhaJayalakshmi in forum Advanced Java
    Replies: 1
    Last Post: 07-23-2008, 11:56 AM
  5. Interacting with the Java Garbage Collector
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-28-2008, 08:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •