-1
public class Node<T> {
    T data;
    Node<T> next;
    Node(T data){
        this.data = data;
    }
}


public  class LinkedListUse{

    public static void print(Node<Integer> head){
        Node<Integer> temp = head;
    
        while(temp != null){
            System.out.print(temp.data +" ");
            temp = temp.next;
        }
        System.out.println();
    }
    
    public static void increment(Node<Integer> head){
        Node<Integer> temp = head;
        while(temp != null){
            temp.data++;
            temp = temp.next;
        }
    }
    
    public static void main(String args[]){
    
        Node<Integer> node1 = new Node<Integer>(10);
        Node<Integer> node2 = new Node<Integer>(20);
        node1.next = node2;
        increment(node1);
        print(node1);
    }
}

Since node1 has been passed by value (and not pass by reference) in the function increment, hence the output should be 10 20 according to me, but the solution is 11 21.

Can you please help me out with the explanation behind this

trincot
  • 211,288
  • 25
  • 175
  • 211
  • In Java, everything is passed by value, but what value is passed? In case of objects is the reference. So it's obvious that object properties can be altered when having the reference. – Traian GEICU May 15 '21 at 17:09
  • 2
    Does this answer your question? [Is Java "pass-by-reference" or "pass-by-value"?](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – Rocco May 15 '21 at 17:28
  • Very general advice: This kind of question is usually solved by placing intermediate output from the program. Add `System.out.println(...)` in the middle of the code to see what is happening. – Luis A. Florit May 15 '21 at 20:57
  • @LuisA.Florit: if the confusion is about the exact nature of passing a reference by nature, then that will only deepen the confusion. They must attempt to fix their misunderstanding of what passing a reference by nature is (and the duplicate question has some great answers that can help with that). – Joachim Sauer May 16 '21 at 10:50

1 Answers1

0

The call to increment will mutate the list. Maybe it helps to visualise the list. After the execution of node1.next = node2 we get this situation:

 node1           node2
  ↓               ↓
┌───────────┐   ┌───────────┐
│ data: 10  │   │ data: 20  │
│ next: ──────> │ next: null│
└───────────┘   └───────────┘

Then increment(node1) will define two more variables that reference node1:

 head
 temp 
 node1           node2
  ↓               ↓
┌───────────┐   ┌───────────┐
│ data: 10  │   │ data: 20  │
│ next: ──────> │ next: null│
└───────────┘   └───────────┘

In its while loop it will first increment temp.data (so it becomes 11), and then move the temp reference to node2:

 head            temp 
 node1           node2
  ↓               ↓
┌───────────┐   ┌───────────┐
│ data: 11  │   │ data: 20  │
│ next: ──────> │ next: null│
└───────────┘   └───────────┘

In the second iteration the data of node2 (= temp) will be incremented, and temp moves to temp.next which is null:

 head                             temp==null 
 node1           node2
  ↓               ↓
┌───────────┐   ┌───────────┐
│ data: 11  │   │ data: 21  │
│ next: ──────> │ next: null│
└───────────┘   └───────────┘

So, although the changes to temp do not affect the reference of node1, any mutation to the referenced nodes will remain, even after the call to increment has finished execution. There is nowhere a value of 10 any more, nor 20. These have been overwritten.

When the print method is called, a similar thing happens as when increment was called, except that now no mutation is brought to the nodes. It will find the nodes as they were left by the call to increment. There are no other nodes.

trincot
  • 211,288
  • 25
  • 175
  • 211