references

Discussion in 'Java' started by justplain.kzn@gmail.com, Jul 24, 2006.

  1. Guest

    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...
    , Jul 24, 2006
    #1
    1. Advertising

  2. jtl.zheng Guest

    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, Jul 24, 2006
    #2
    1. Advertising

  3. Guest

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


    jtl.zheng wrote:
    > 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
    , Jul 24, 2006
    #3
  4. jtl.zheng Guest

    yes..that's right
    : )
    jtl.zheng, Jul 24, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roger Leigh
    Replies:
    8
    Views:
    436
    Karl Heinz Buchegger
    Nov 17, 2003
  2. Replies:
    3
    Views:
    449
    Victor Bazarov
    Nov 10, 2004
  3. DanielEKFA
    Replies:
    8
    Views:
    606
    DanielEKFA
    May 16, 2005
  4. Replies:
    8
    Views:
    711
    Bruno Desthuilliers
    Dec 12, 2006
  5. Lars Willich
    Replies:
    13
    Views:
    835
    Ian Shef
    Oct 23, 2007
Loading...

Share This Page