0
 public void swap(Point var1, Point var2)
 {
  var1.x = 100;
  var1.y = 100;
  Point temp = var1;
  var1 = var2;
  var2 = temp;
 }

public static void main(String [] args)
{
  Point p1 = new Point(0,0);
  Point p2 = new Point(0,0);
  System.out.println("A: " + p1.x + " B: " +p1.y); 
  System.out.println("A: " + p2.x + " B: " +p2.y);
  System.out.println(" ");
  swap(p1,p2);
  System.out.println("A: " + p1.x + " B:" + p1.y); 
  System.out.println("A: " + p2.x + " B: " +p2.y);  
}

Running the code produces:

A: 0 B: 0
A: 0 B: 0
A: 100 B: 100
A: 0 B: 0

I understand how the function changes the value of p1 as it is passed-by-value. What i dont get is why the swap of p1 and p2 failed.

bouncingHippo
  • 5,940
  • 21
  • 67
  • 107
  • 2
    Draw a picture with boxes and arrows. `p1` and `p2` and `var1` and `var2` are four distinct variable boxes. But there will only be two object boxes. When you trace out the evolution of your code with pictures and you get it right, you will likely be enlightened much more than by reading textual descriptions of the scenario. – Ray Toal Jun 19 '12 at 14:20

4 Answers4

4

Java is pass by value at all times. That's the only mechanism for both primitives and objects.

The key is to know what's passed for objects. It's not the object itself; that lives out on the heap. It's the reference to that object that's passed by value.

You cannot modify a passed reference and return the new value to the caller, but you can modify the state of the object it refers to - if it's mutable and exposes appropriate methods for you to call.

The class swap with pointers, similar to what's possible with C, doesn't work because you can't change what the passed reference refers to and return the new values to the caller.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • 1
    +1 Please see this discussion for more insightful answers http://stackoverflow.com/questions/40480/is-java-pass-by-reference – sperumal Jun 19 '12 at 14:25
  • "You cannot modify the reference itself" You *can* modify a reference to point to a different object. That's what's happening inside the swap function. But that does not modify the reference in the original function, because Java is pass by value – newacct Jun 19 '12 at 17:53
  • No, you can't return that to the caller. Perhaps my wording isn't clear; I'll revise. – duffymo Jun 19 '12 at 17:57
3

myFunction() just swaps the references to both objects inside the function (which are distinct from those outside), but does not swap the references outside the function nor the object instances themselves!

Sirko
  • 72,589
  • 19
  • 149
  • 183
2

There would be a copy of reference generated in formal argument place, So there would be a new reference pointing to instances of Point

jmj
  • 237,923
  • 42
  • 401
  • 438
0

Objects are assigned by reference, not cloned, but the arguments stay as references to the actual objects, they will not turn into "references to references".

What I mean is that var1 reference the same object as p1 initially, but then you change var1 to reference another object. This does not affect what object p1 references because var1 is NOT a reference to p1, only a reference to the same object as p1 is referencing.

Emil Vikström
  • 90,431
  • 16
  • 141
  • 175