Results 1 to 4 of 4
  1. #1
    pierxiu is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default pass Object by reference and garbage collection

    Hello everyone. Thank you for reading and trying to answer my question.
    I am working on a tutorial on implementing stack using linked list(Data Structures and Algorithms) and I get some questions.
    The questions are a bit complicated and I will try my best to make my questions as clear as possible.

    I will give a introduction to my problem first before I post my code so I hope you can have a better understanding.
    Now, I have an object (let's call it q) and in this q object there is only an instance variable which is also an object called "first"
    i.e. q -> first = "123";
    In a function call "pop", the function will accept the object q and erase all its content, that is "first".
    i.e. q = null;
    Now q becomes a null object. Then, the function will assign some new value of "first" to object q;
    i.e. q -> first = "456";

    In my opinion, object q is passed by reference to the function, that means if I change the contents of q, the variable outside at the main will still be affected.
    However, when I trace my program using the eclipse, I find that when the contents of the q is erased, and new contents are assigned, the variable id of q has changed(Is this garbage collection?).
    And at the main, q becomes null and doesn't hold the new value of "first".
    I would like to know is this normal? Or I have do it wrongly giving this strange result? Here are the related code(fraction).

    Function pop:
    Java Code:
    		
    LinkedQueue temp = new LinkedQueue();
    if(q.isEmpty()){
    	return "Empty Stack! Do not pop.";
    }
    Object targetElement = q.getRearElement();
    while(!q.isEmpty()){
    	Object removedElement = q.remove();
    	if(!q.isEmpty()){
    		temp.put(removedElement);
    	}else{
    		break;
    	}
    }
    q = temp;
    return targetElement;
    While q is the object I mention above.
    One more thing to add is that when I changed the last 2 lines to
    Java Code:
    		
    q.front = temp.front;
    return targetElement;
    Then, everything works again!

    I hope my explanation is not too confusing and once again thank you very much.
    Last edited by pierxiu; 03-28-2012 at 05:18 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,923
    Rep Power
    8

    Default Re: pass Object by reference and garbage collection

    I'm not really sure what you're asking. Java passes everything by value, not by reference. What you're talking about is the value of a reference, which sounds confusing but really isn't.

    I highly recommend you read this: JavaRanch Campfire - Cup Size: a Story About Variables and its follow-up: JavaRanch Campfire - Pass By Value, Please before continuing the discussion.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    pierxiu is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default Re: pass Object by reference and garbage collection

    Thank you very much for your answer.
    I think I had misunderstanding on the Java variable-passing. I initially thought that Java is similar to C++ and Object and array are passed by reference.
    But the two web pages you have given me does help me a lot and now I have solved my question.
    Last edited by pierxiu; 03-28-2012 at 06:20 PM.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,923
    Rep Power
    8

    Default Re: pass Object by reference and garbage collection

    Cool, I'm glad you got it sorted out.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Garbage Collection - Self reference, static and non-static.
    By garyiskidding in forum Advanced Java
    Replies: 10
    Last Post: 03-17-2011, 11:22 AM
  2. JVM Garbage Collection
    By daromnet in forum Advanced Java
    Replies: 4
    Last Post: 03-09-2011, 10:18 AM
  3. Garbage Collection Question
    By DrMath in forum New To Java
    Replies: 5
    Last Post: 10-03-2009, 10:08 AM
  4. How setting an Object to null help Garbage Collection?
    By piyu.sha in forum Advanced Java
    Replies: 3
    Last Post: 10-06-2008, 07:35 AM

Posting Permissions

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