1

This gif was retrieved from https://blog.penjee.com/passing-by-value-vs-by-reference-java-graphical/

An animation depicting pass-by-value vs pass-by-references

Maybe I'm terribly misunderstanding this, but please bear with me.

According to my understanding, when we define a variable in java, we are essentially creating a pointer to that object that is created. i.e.

Object objPointer = new Object();

Here objPointer is reference to the Object that was created, not the object itself. And when we use this object as an argument in a method:

void foo(Object newPointer){
    //newPointer points to a copy of the Object objPointer pointed to.
}

foo(objPointer);

The formal argument newPointer is pointer to a copy of the value of the object that is passed as an argument. This is why swap methods don't work in java.

But my question is this: If the formal argument only points to a copy of the original object, then why can we change the values of the properties of that object? i.e.

class Object{
   int var = 0; //default value of 0
   void setVar(int newValue){
      this.var = newValue;
   }
}

void foo(Object newPointer){
    newPointer.setVar(1); 
}

Object objPointer = new Object();
//The Object objPointer points to has a var value of 0 as default.

foo(objPointer);

/*
       After foo is called, the var value of objPointer has changed to 1
     although the setVar method should only change the value of var for the copy 
     of that object that newPointer points to.
*/

I hope what I'm asking makes sense, the gif I found kind of illustrates what I'm saying: How are the properties of the cup Object changed, if fillCup only changes the properties of the copy?

Rahul Chowdhury
  • 641
  • 1
  • 7
  • 16
  • 1
    The copy is the value, which is the pointer to the same reference (i.e. there aren't two instances). – Elliott Frisch May 07 '17 at 01:21
  • 2
    The "pointer" is the value being passed. There's still only one instance of `Object` on the heap, but there's a copy of the "pointer" made in the stack. – David May 07 '17 at 01:21
  • 1
    The pointer gets copied, but both pointers point to the same object. – Alexander Torstling May 07 '17 at 01:30
  • Since you can only pass pointers or primary values in java, you will never copy objects when calling function s. – Alexander Torstling May 07 '17 at 01:31
  • _References are passed by value._ – Louis Wasserman May 07 '17 at 01:37
  • The test isn't whether the object pointed to by the pointer can be changed, @RahulChowdhury, but whether the caller sees a change if the parameter is reassigned. So not `newPointer.setVar(1);` but `newPointer = new Pointer();`. You have to use the correct test. The test you posted only shows pass-by-value of pointers. – Lew Bloch May 07 '17 at 04:13

2 Answers2

2

When you pass the objPointer reference to the foo method, the foo method obtains a copy of the object reference, through this reference, it can access or mutate the parameter object. However, if you change the object to which the parameter refers i.e. a new object then this will not affect the initial object that was first passed in.

Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
0

You're passing the copy of the reference which still points to the same object in the memory. You are not passing a new object. You are passing the reference to the objPointer that you created at line: Object objPointer = new Object();

qrius
  • 621
  • 2
  • 9
  • 22