Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Pass by value

 
Prannav Santhosh
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was going thorugh an artical in javaworld and Can any one help me in under standing this below concept ?

public void tricky(Point arg1,Point arg2){
arg1.x=100;
arg1.y=100;
Point temp= arg1; //1
arg1=arg2; //2
arg2=temp; //3
}


public static void main(String args[]){
Class2 c = new Class2();
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("");
c.tricky1(pnt1,pnt2);
System.out.println("X: "+pnt1.x +" Y:"+pnt1.y);
System.out.println("X: "+pnt2.x +" Y:"+pnt2.y);

}
The out put of the above program is
X: 0 Y:0
X: 0 Y:0

X: 100 Y:100
X: 0 Y:0

My question:
-------------
public void tricky(Point arg1,Point arg2){
arg1.x=100; //1
arg1.y=100; //2
Point temp= arg1; //3
arg1=arg2; //4
arg2=temp; //5
}

when we are calling the tricky method , we are passing the copy if the reference value to the method (copy of pnt1 and pnt2) and now we have arg1 and pnt1 pointing to the same Point object and similarly we have pnt2 and arg2 pointing to the same Point Object.
at //1 and //2 we are seeting x and y to a new value.
at //3 the copy of the reference variable arg1 is assigned to temp reference variable (Does it mean the arg1 , temp and pnt1 are pointing to the same Point Object ?)
at //4 copy of the arg2 is assigned to reference variable arg1 (which means the arg1.x and arg1.y in the main should print the value as zero?).
at //5 copy of the reference variable temp is assinged to arg2 (which means the arg2.x and arg2.y in the main should print the value as 100?).

Can some one explains me why i see a different out as shown below ?
The out put of the above program is
X: 0 Y:0
X: 0 Y:0

X: 100 Y:100
X: 0 Y:0

Thanks in advance.

Regards,
Prannav.



 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well when the program execution enters the tricky method, there are four references and two objects. So within the method, though the local references change or swapped doesn't mean the pnt1 and pnt2 are swapped. arg1 and arg2 are local references and their life is over as soon as the execution of the method is over. Also the references are not returned to have any impact on main's local references. Thus, arg2.x and arg2.y will have 100 but not pnt2.x and pnt2.y. I guess, a object graph will solve this type of question in a clear way(same way we solve garbage collector questions).

[Edit: removed long quotes]
 
Prannav Santhosh
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont agree with you, because take a look the below code.

public void tricky1(Point arg1,Point arg2){
Point arg3 =arg1;
arg3.x=300;
arg3.y=300;


}

Then if i have one more method and from the main() method if i call tricky1() instead of tricky() , i see the below output.

X: 0 Y:0
X: 0 Y:0

X: 300 Y:300
X: 0 Y:0
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prannav please Use Code Tags when you post a source code. You can edit your message using button and then add code tags to it.

Read this and this...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic