Results 1 to 5 of 5
  1. #1
    Krooger is offline Member
    Join Date
    Nov 2008
    Posts
    43
    Rep Power
    0

    Question Java pointers? How to...

    I have an instance of a Node class and I have:
    Java Code:
    Node myNode = someOtherNode;
    So as far as I know myNode is pointing to someOtherNode not making a copy.
    I now need to set the "someOtherNode" to null but the only referance I have to it is myNode.

    Java Code:
    myNode = null;
    This should not effect "someOtherNode" from what I understand it will only destroy the pointer? Is this correct and if so is there a way to set what it is POINTING to, to null?

    Somthing like how C uses:
    Java Code:
    nodePointer* = null;
    I dont remember if thats the C syntax but the idea is that null will apply to whatever it is pointing at.

    Any tips? Thank you!

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default

    You have a problem telling the difference between an object and a pointer. myNode and someOtherNode are both pointers.
    myNode is pointing to someOtherNode not making a copy.
    No quite.
    The assignment statement sets myNode to point to the same object as someOtherNode points to.
    I now need to set the "someOtherNode" to null but the only referance I have to it is myNode.
    someOtherNode is a pointer, not an object. You can set its value to null. myNode will still point to the object.
    is there a way to set what it is POINTING to, to null?
    Invalid concept. Objects can't be set to null. Their instance variables can be set to null, but the object will still exist.
    Objects are collected by the garbage collector when there are no pointers referencing them.

  3. #3
    Krooger is offline Member
    Join Date
    Nov 2008
    Posts
    43
    Rep Power
    0

    Default

    Thank You for the help, I understand now how pointers work. When I needed to delete a node somewhere else in my binary tree, in the algorithm I had a pointer following one node behind and once I found the node I used the previouse node to do somthing like:
    Java Code:
    previouseNode.leftNode = null;
    This seemed to work well, aside from the tree breaking apart so I also checked to see if it was a leaf node and if it was not I replaced it's values with the approriate node one to the left or the right and then as far right or left from there as I could go. Then if need be re connected the new node that is being removed back up again if it had children on the other side!

    I hope I described what I did well enough for my next question...
    This all worked and my program is fine but it seems very long and unnessesary, but they only way I can think of. There are so many cases my delete function became quite large and hard to read with lots of if then and while loops. (I'm not allowed to use recursive for this project) Is this a standard meathod? or is there somthing better?

    Thank You.

  4. #4
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    Honestly I'm sure there is another way to do this(recursion being one) but I think this project could be the introduction to binary trees or something and as long as it works that's fine. Your next project might be to make the code more efficient and you could be asked to use recursion in which case you want to be able to improve your code.

    For homework assignments and projects and such I always figured as long as it worked and did what it was supposed to it was fine. They were assignments to teach me an idea, it's not going to be perfect the first time through. Now if this is a real world project that will actually be implemented then yes your probably going to have to enhance it a bit.

  5. #5
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    Like Norm said, you can have a null reference, but you cannot have a null object. You can add an "isnull" field to your class.

    The only reason I would see for you to do this is if there are multiple references to the same object such that a method or thread operating on one of those objects, namely delete it, should affect all other references to it. Though perhaps a more appropriate representation of this would be to make a list of references to specific objects.

Similar Threads

  1. Pointers
    By ravian in forum New To Java
    Replies: 5
    Last Post: 11-28-2007, 01:49 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
  •