Skip to main content
gamedev.net
🔒 Locked

General pointer/object question in Java

Started by bioagentX Feb 18, 2005 at 5:42 PM 2 replies 950+ views
Original Post
bioagentX
bioagentX
Please look at code for question
[SOURCE]

//Suppose I have the following relationship with nodes containing strings

Node node0 = new Node("Random String", null)
                      //the first parameter is for the value of the node, and
                     //the second parameter is where that node points

 Node node1 = node0

 Node node2 = node1;

 node1 = null;

 //My question is, will node2 now point to Node0? 
//If so, how is that possible if node1 is no longer referring to node0?


          








[/SOURCE]
[/source][/source]
There are three types of people in this world, those who can count, and those who can't
Trap
Trap
node2 doesn't refer to node1, it does refer to the same object as node1. Same with node0 and node1
kinglink
kinglink
If you know anything about C++ pointers. Most everything in Java (except primitives if I remember right) is done as pointers.

Basically every Identifier is a pointer. so every one takes an address of data, not the address of pointers (unless you do something other then normal.)

So IF Node0 created his data at location 30

Node0=points to 30 at first

then Node1=points to 30 (also)

Then Node2=points to 30

Finally Node1 points to Null. Because Node1 changed AFTER node 2 is assigned, Node 2 is still pointed at the data

Node0 is 30.
Node1 is Null
Node2 is 30.

I'm not sure if I'm explaining this correctly but feel free to ask about any area where I've confused you, I'll try to help you more there.
Zahlman
Zahlman
node0, node1 and node2 are all actually pointers; well, perhaps more like references, except they can be null. (However, they are guaranteed to be either null or point to a valid object.)

Before the setting to null, node0, node1 and node2 all point at the same structure in memory, which contains a String (which in turn is a pointer to some other structure in memory), some null pointer corresponding to that second ctor argument (I am blindly assuming that both of the ctor arguments correspond to actual data members of Node), possibly other data members, and bookkeeping stuff (an int or three used for reference counting for the garbage collector, and for allowing other objects to "lock" on this object for thread synchronization). You can never access this chunk of memory as a raw block of bytes, but only through appropriately typed object references. When you write for example "node0.foo()", this is really like C++ "node0->foo()". By adding some indirection, the Java language ends up making memory layout more uniform (for lack of a better word), and thus it can offer you a more uniform syntax (you don't have to worry about . vs ->, or indeed about all that other &*&*ing nonsense...).

After setting to null, node1 is null, and attempting to use it will throw NullPointerException (because internally, there will be code generated to dereference the pointer). The other variables still point at the structure they were pointing at before. Since the reference count is still non-zero, the object will not be GC'd (unless the node0 and node2 pointers are lost, or for some reason need to be GC'd as well, for example if there is a cycle detected by the GC).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.