references

J

justplain.kzn

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

jtl.zheng

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
 
J

justplain.kzn

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top