1

While playing in Java. I saw different behaviour if an object is modified and given a value and different value if it is assigned a new object. Here is code that I made to show the result.

public class Test {

    int i;

    public Test(int j) {
        this.i = j;
    }

    public static void main(String[] args) {

        Test A = new Test(5);
        Test N = new Test(5);
        add(A);
        makeNew(N);
        System.out.println("Value of A.i= "+A.i);
        System.out.println("Value of N.i= "+N.i);

    }

    private static void add(Test t) {
        t.i+= 3;
        System.out.println("Inside method add() t.i= "+t.i);

    }

    private static void makeNew(Test t) {

        t = new Test(8);
        System.out.println("Inside method makeNew() t.i= "+t.i);

    }

}

Here is the output of the above code.

Inside method add() t.i= 8
Inside method makeNew() t.i= 8
Value of A.i= 8
Value of N.i= 5  

In above example object A is modified to value 8. And object B is given a new object itself. But calling them back only object A shows new value. Object B shows the old value itself. Should not they be showing same value because both case are pass by refernce? I was expecting same value for A.i and N.i.

ollo
  • 24,797
  • 14
  • 106
  • 155
vincent mathew
  • 4,278
  • 3
  • 27
  • 34
  • possible duplicate of [Is Java "pass-by-reference"?](http://stackoverflow.com/questions/40480/is-java-pass-by-reference) – Raedwald May 19 '13 at 09:12

6 Answers6

3

Here's what happens:

Test A = new Test(5);
Test N = new Test(5);

enter image description here

add(A);  // method is add(Test t)

enter image description here

makeNew(N)// method is makeNew(Test t)

enter image description here

t = new Test(8);

enter image description here

System.out.println("Value of A.i= "+A.i);
System.out.println("Value of N.i= "+N.i);

enter image description here

Jops
  • 22,535
  • 13
  • 46
  • 63
1

In your makeNew, you're overwriting the reference to the existing object (that's passed in as the paramter) with your new test(8) object. However, that's local inside makeNew, so the original object sitting inside main(...) is not affected.

Stochastically
  • 7,616
  • 5
  • 30
  • 58
1

Whenever you make a variable equal to an object and later use new on that object somewhere else like through another reference, your variable you set to the objects reference no longer points to whatever the new object is, but still holds onto the old. So if multiple variables at different scopes hold a reference, they all need a way to have them made equal to whatever the new object is or they no longer are in synch.

LanternMike
  • 664
  • 1
  • 5
  • 16
1

I think this will make you doubt clear:

Before assigning t inside makeNew to another object

After you assign t to a new object

You see N still point to the first object

Abubakkar
  • 15,488
  • 8
  • 55
  • 83
0

Java is pass-by-value. You pass the reference of an object as a value, and you can thus modify that object. However, you cannot modify the actual reference of an object and make it point to something else.

Your question has already been answered here: Is Java "pass-by-reference" or "pass-by-value"?

Community
  • 1
  • 1
  • Java is not pass by value. Only primitives are passed by value. The reason the OP's object doesnt change state is because the paramter is ANOTHER reference to the object and `makeNew` is just setting the `t` reference to another new object which doesn't affect `N` – Matthew Cox May 19 '13 at 08:57
  • No, your vote down is incorrect. Look at the first answer of my link. – Matthias Van Eeghem May 19 '13 at 09:00
  • Java is always pass by value. http://stackoverflow.com/questions/40480/is-java-pass-by-reference – Kyle May 19 '13 at 09:01
  • This is an internal semantic you are referring to in this link. Upon reading further, all you are demonstrating is that yes, the memory location address is passed by value ... well of course it is! Obviously the same could be said of a pointer in any other given language. When we talk about references, we simply say that objects are passed by reference (in subtext not bothered to be mentioned: by virtue of a memory address that is actually passed by value). This is being pedantic at it's finest and it doesn't help the OP really understand the answer to his question. – Matthew Cox May 19 '13 at 09:11
  • This "internal semantic" helped me understand what happened when passing objects to functions, so I'm guessing it will help him as well. – Matthias Van Eeghem May 19 '13 at 09:13
-1

In Java you do not pass the actual object nor do you pass the reference to the object. You pass copy of the reference to that object. Now when you say

makeNew(N);

N which is the reference to new Test(5) is not passed but the copy of it's reference is passed. In the makeNew() function this copy points to some new object and print the value appropriately but the N will still point to the original object.

Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289