1

A while ago I implemented a LinkedList in C. It was pretty straight forward since each Node was made of a value and another pointer to the next element in the list. But now I am trying to implement a LinkedList in Java, since there are no pointer I made a Node like this:

public class MyNode{
    int value;
    MyNode next;

    public MyNode(int i){
        value = i;
        next = null;
    }
}

Now lets say I have this:

MyNode n1 = new MyNode(1);
MyNode n2 = new MyNode(2);
n1.next = n2;
MyNode n3 = new MyNode(3);
n2 = n3;

What would I get if I do

System.println(n1.next.value);

Would I get a 2 or a 3? My main problem coming from C is that I do not really understand how java moves around the data. Does it copy n2 to n1 or does n1 just point to n2? This is all really confusing.

user2278279
  • 65
  • 1
  • 7
  • 4
    *Would I get a 2 or a 3?* => Why don't you try? – assylias Apr 16 '13 at 05:15
  • You are completely right. I should have tried it before I asked, still I would like to know how Java handles the line "n1.next = n2;" – user2278279 Apr 16 '13 at 05:17
  • This might help: http://stackoverflow.com/questions/10042/how-do-i-implement-a-linked-list-in-java – Hiny Apr 16 '13 at 05:17
  • @Hiny please **read** the question at the bottom of OP body. – Luiggi Mendoza Apr 16 '13 at 05:18
  • This and related questions are very well explained in Java tutorials. See [StackOverflow Java wiki](http://stackoverflow.com/tags/java/info) to get a Java introduction and a list of available online tutorials on the net. – Luiggi Mendoza Apr 16 '13 at 05:21
  • Java has LinkedList builtin so I assume this is just an exercise. It might be worth reading the source of how Java does it as a learning exercise. – Peter Lawrey Apr 16 '13 at 07:07

3 Answers3

5

To try to make an analogy with pointers (we call them references in Java):

You can think of variables (n1, n2, n1.next etc.) as pointers to objects somewhere in memory. So new Node(2); creates a Node and places it in memory and n2 = ... makes the variable n2 point to the "address" of that node.

When you then write n1.next = n2;, you make n1.next point to that same "address" by copying the value of that address into n1.next.

When you later write n2 = n3;, you make the n2 variable point to a different "address", but n1.next is still pointing to the address of your initial new Node(2) object.

Bottom line: System.out.println(n1.next.value); prints 2, not 3.

A related post: Is Java "pass-by-reference" or "pass-by-value"?

Community
  • 1
  • 1
assylias
  • 321,522
  • 82
  • 660
  • 783
1

The response is 2.

n1.next will continue to point to your instance MyNode(2); After n2 = n3; n2 points to the n3 but you will keep a reference for the instance MyNode(2) on n1.next.

The GarbageCollector only will collect the objects instances that don't have any references to them. In this case you will continue to reference the instance (MyNode(2)) on n1.next

Pedro Gandola
  • 196
  • 1
  • 11
0

In java the variables are always passed by values. Java passes objects as references passed by value.

For example, your code

MyNode n1 = new MyNode(1);
MyNode n2 = new MyNode(2);
n1.next = n2;
MyNode n3 = new MyNode(3);
n2 = n3;

Is executed in this way.

Create new node as n1 with value 1

create new node as n2 with value 2

Set the object n1.next to n2

Create new node as n3 with value 3

Replace object referenced by n2 with the one referenced by n3 (It doesn't replace the reference of n1.next

So your call for

System.out.println(n1.next.value);

prints 2.

Sri Harsha Chilakapati
  • 11,744
  • 6
  • 50
  • 91