If Java uses the pass-by reference, why won't a swap function work?
Your question demonstrates a common error made by Java language newcomers. Indeed, even seasoned veterans find it difficult
to keep the terms straight.
Java does manipulate objects by reference, and all object variables are references. However, Java doesn't pass method arguments by reference; it passes them by value.
Take the badSwap() method for example:
public void badSwap(int var1, int var2)
{
int temp = var1;
var1 = var2;
var2 = temp;
}
When badSwap() returns, the variables passed as arguments will still hold their original values. The method will also fail if we change
the arguments type from int to Object, since Java passes object references by value as well. Now, here is where it gets tricky:
public void tricky(Point arg1, Point arg2)
{
arg1.x = 100;
arg1.y = 100;
Point temp = arg1;
arg1 = arg2;
arg2 = temp;
}
public static void main(String [] args)
{
Point pnt1 = new Point(0,0);
Point pnt2 = new Point(0,0);
System.out.println("X: " + pnt1.x + " Y: " +pnt1.y);
System.out.println("X: " + pnt2.x + " Y: " +pnt2.y);
System.out.println(" ");
tricky(pnt1,pnt2);
System.out.println("X: " + pnt1.x + " Y:" + pnt1.y);
System.out.println("X: " + pnt2.x + " Y: " +pnt2.y);
}
If we execute this main() method, we see the following output:
X: 0 Y: 0 X: 0 Y: 0 X: 100 Y: 100 X: 0 Y: 0
The method successfully alters the value of pnt1, even though it is passed by value; however, a swap of pnt1 and pnt2 fails! This is the major source of confusion. In the main() method, pnt1 and pnt2 are nothing more than object references. When you pass pnt1 and pnt2 to the tricky() method, Java passes the references by value just like any other parameter. This means the references passed to the method
are actually copies of the original references. Figure 1 below shows two references pointing to the same object after Java passes an object to
a method.

Figure 1. After being passed to a method, an object will have at least two references
Java copies and passes the reference by value, not the object. Thus, method manipulation will alter the objects, since the references point to the original objects. But since the references are copies, swaps will fail. As Figure 2 illustrates, the method references swap, but not the original references. Unfortunately, after a method call, you are left with only the unswapped original references. For a swap to succeed outside of the method call, we need to swap the original references, not the copies.

Figure 2. Only the method references are swapped, not the original ones
yeah, you are rightBy Anonymous on March 11, 2010, 3:32 pman argument can be either an address of an object/primitive or an object/primitive itself. when an object/primitiv is passed, it is always copied/duplicated on the...
Reply | Read entire comment
ok. thanks. so do I have it straight now?By Gijera on March 10, 2010, 10:30 amOk, so I'm right, in that the objects address is passed. And that a primitive's type-value is passed. But the difference is that with an object the address that...
Reply | Read entire comment
your 'obj value' is NOT passed by valueBy Anonymous on March 9, 2010, 10:12 amin case of an object, the 'value', which is an address, is NOT passed by value because the 'value' is NOT duplicated. in case of a primitive, the value is passed...
Reply | Read entire comment
So do I have this straight?By Gijera on March 5, 2010, 11:43 pm- An objects value is its reference address, a primitives value is its value, and arguments are passed by value (whether that be an 'object value' or a 'primitive...
Reply | Read entire comment
helpBy Anonymous on March 4, 2010, 12:21 amimport java.io.*; import java.util.*; class h{ static void gcd(long a, long b,long &x,long &y,long gc) { if(b==0) { gc=a; x=1; y=0; return; } gcd(b,a%b,x,y,gc); long...
Reply | Read entire comment
View all comments