0

See the test code:

class Node
{
    int num = 0;
}

class TestPassByReference
{
    void increaseByOne(Node N)
    {
        N.num++ ;
    }

    public static void main(String args[])
    {
        Node N = new Node() ;
        TestPassByReference T  = new TestPassByReference() ;
        T.increaseByOne(N) ;
        System.out.println("num = "+N.num) ;
        T.increaseByOne(N) ;
        System.out.println("num = "+N.num) ;
        T.increaseByOne(N) ;
        System.out.println("num = "+N.num) ;
    }
}

Which gives the result:

num = 1
num = 2
num = 3

The problem here, is that since Java is call by value then the num attribute of Node should have remained 0.

I think that this is due to creation of an object T, which would have contained an object N. But there is no attribute in T to save a Node like object. So how is this happening?

Any help is nefariously appreciated.

Moon.

aUserHimself
  • 1,589
  • 2
  • 17
  • 26
Mooncrater
  • 4,146
  • 4
  • 33
  • 62
  • 1
    Did you see this: http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value ? – Matt Mar 16 '17 at 07:25
  • N is Called-by-value. When you reference the value with the dot, you leave the copied value which is an object reference and reference the object. This is called pointers in other languages. – Thorbjørn Ravn Andersen Mar 16 '17 at 07:26
  • 4
    Possible duplicate of [Is Java "pass-by-reference" or "pass-by-value"?](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – piet.t Mar 16 '17 at 07:27

4 Answers4

4

Java passes objects by reference. Node N is an object. So when you do N.num++ you're incrementing the same object's integer.

vallentin
  • 23,478
  • 6
  • 59
  • 81
2

Java is call-by-value in the sense that references are passed-by-value.

This means you can change the internal state of the object you passed N (e.g. by modifying the num field's value) but you cannot reassign N to a new object (e.g. N = null; or N = new Node();).

class Node
{
    int num = 0;
}

class TestPassByReference
{
    void increaseByOne(Node N)
    {
        N.num++ ;
        System.out.println("num value is:" + N.num);
    }

    void increaseByOneThenChange(Node N)
    {
        // increase by one as before
        N.num++;
        // change the reference to a new object
        N = new Node();
        System.out.println("num value is:" + N.num);
    }

    public static void main(String args[])
    {
        Node N = new Node() ;
        TestPassByReference T  = new TestPassByReference() ;
        T.increaseByOne(N) ;
        System.out.println("num = "+N.num) ;
        T.increaseByOne(N) ;
        System.out.println("num = "+N.num) ;
        T.increaseByOne(N) ;
        System.out.println("num = "+N.num) ;

       // now try this method to see the difference
       T.increaseByOneThenChange(N);
       // N here is the original object, whose `num` value was incremented
       // but the reference remains unchanged by the above method
       System.out.println("num = "+N.num) ;
    }
}

In the second case, only the reference that was passed to the method changes thus being visible only in the method's body (N will no longer refer the original object here), but the original one remains the same.

aUserHimself
  • 1,589
  • 2
  • 17
  • 26
0

In your code you have single Object of class Node .

That holds value of num with initial value 0, And when call function and increment value of num each time value of num will be increase because same Node class object . its simple to understand node object is instance of class Node if you make another Node class object it will different instance of class .

class Node
{
    int num = 0;
}
class TestPassByReference
{
    void increaseByOne(Node N)
    {
        N.num++ ;
    }
    public static void main(String args[])
    {
        Node N1 = new Node() ;
        Node N2 = new Node() ;
        TestPassByReference T  = new TestPassByReference() ;
        T.increaseByOne(N1) ;
        System.out.println("num = "+N1.num) ;
        T.increaseByOne(N1) ;
        System.out.println("num = "+N1.num) ;
        T.increaseByOne(N2) ;
        System.out.println("num = "+N2.num) ;
    }
}

Then out come is :

num = 1
num = 2
num = 1
Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
0

well, you are creating single object of Node and TestPassByReference so T referes the same Node reference which you cloud create earlier. So the same value is increaments when you call the method.

Vikram Pawar
  • 128
  • 7