1

I've been learning Java over the past year and I've gotten fairly proficient with data structures, but there's something that's always been on my mind that I've never quite figured out. The following is an example:

public class SList{
    private SListNode head;
    private int size;

    public void insertEnd(Object obj) {
        if (head == null) {
            head = new SListNode(obj);
        } else {
            SListNode node = head;
            while (node.next != null) {
                node = node.next;
            }
            node.next = new SListNode(obj);
        }
        size++;
  }

Assuming SListNode and the SList constructor have already both been implemented, why is it that the "head" reference changes and has a Node added to its end while there was no declaration like head = node; at the very end of the method? I know this is probably very basic, but I've been looking for a while and there's never been an explanation.

kraskevich
  • 18,368
  • 4
  • 33
  • 45
Iche
  • 145
  • 2
  • 8
  • SListNode node = head; at this line both are referring to same object. so if you change anything in object it will impact.. – Prashant Jan 05 '15 at 19:00
  • I'm not sure if I understood the question; why would you expect something like `head = node` at the end of the `insertEnd` method? – Codor Jan 05 '15 at 19:02
  • so then when you say node = node.next, you're not changing the head node. But then when you say node.next = SListNode(obj), you are changing the head node? How is that? – Iche Jan 05 '15 at 19:04
  • My point was that to make a change to head, you have to assign its reference to something. – Iche Jan 05 '15 at 19:05
  • The SListNode object is clearly mutable. You can change its `next` field without substituting a new reference. When `head == node`, setting the `next` field is mutating the `head` object; when there are several nodes in the list, you don't modify the `head` but instead you modify the former last node in the list so its `next` points to the new next node in the list. – David Conrad Jan 05 '15 at 19:07
  • You do not have to assign a new reference to `head` to change it -- not if it is a mutable object. You can just set its fields (such as its `next` field) to new values. – David Conrad Jan 05 '15 at 19:08
  • Keep in mind that linked lists will cause you to go mad. – Hot Licks Jan 05 '15 at 19:25

1 Answers1

1
SListNode node = head;

In this line node starts to point to the same object as a head is. So when you do something with node you are changing object itself by link. So just keep in mind that in java everything is pointers in order to understand where you are doing a mistake.

In general I think you want to receive something like this (just kickoff sample with main idea):

  public void insertEnd(Object obj) {
        if (head == null) {
            head = new SListNode(obj);
        } else {
            SListNode node = head.next;
            if (node != null {
              while (true) {
                  node = node.next;
                  if (node == null) 
                     break;
              }
            }
            node = new SListNode(obj);
        }
        size++;
  }
Divers
  • 9,531
  • 7
  • 45
  • 88
  • 1
    @Prashant I'm congratulate you! – Divers Jan 05 '15 at 19:07
  • Ok, I'm understanding that now. But than how is it that when you say node = head.next and change the value of the node reference, you are not changing the head variable? – Iche Jan 05 '15 at 19:51
  • @Iche my code was wrong - I fixed it now. But anyway, idea is very simple. If you do a = b, and then change the field by link, e.g. a.field = 1; then you changing the object itself. In our case I don't change head object, I assighn new SListNode(obj) to head.next (which is null). – Divers Jan 05 '15 at 20:52
  • Ah, thank you for saying that. If you change the variable of an object that references head, than the variable of head changes. But if you directly change the reference of the 2nd object (node in this case), then head stays the same and the 2nd object (node) changes. You and Prashant basically said the same thing very simply, but you went into a little more detail. Thank you everyone! – Iche Jan 06 '15 at 02:12
  • Sorry, I'm new to stackoverflow. How do I do that? – Iche Jan 07 '15 at 03:00
  • @Iche Point your mouse to number near my answer, then click on appeared "check". Btw, take this tour also - http://stackoverflow.com/tour – Divers Jan 07 '15 at 12:34