Hi,
I am new to java programming and seem to have got myself into a bit of
confusion with assigning and using reference variables.
Below is a snippet of code I an using for a linkedlist.
public class LinkedList
{
private Node start;
public LinkedList()
{
start=null;
}
public Node find (int x)
{
if (start==null || start.data>0)
return null;
Node p = start; //*************** help
while (p.next != nul && !(p.next.data > x))
p=p.next;
return p;
}
...
...
}
Can some please point out and explain to me why when assigning
reference 'start' to p and then using p within the while loop, start is
NOT affected/changed?
thanks in advance...
jtl.zheng - 24 Jul 2006 05:20 GMT
guest first the 'start' point to a object called "Oa"
Node p = start; // make the "p" point to what the start point to, it's
"Oa"
p=p.next; // at this time the p point to another object
but the 'start' still point to "Oa"
no one change it
justplain.kzn@gmail.com - 24 Jul 2006 05:26 GMT
Ah - thanks a million. I now see that p and start point to the same
just once. p.next is actually a NEW OBJECT reference...
thanks again...
> guest first the 'start' point to a object called "Oa"
> Node p = start; // make the "p" point to what the start point to, it's
> "Oa"
> p=p.next; // at this time the p point to another object
> but the 'start' still point to "Oa"
> no one change it
jtl.zheng - 24 Jul 2006 05:31 GMT