aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Pass by value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Pass by value" Watch "Pass by value" New topic
Author

Pass by value

Prannav Santhosh
Greenhorn

Joined: May 09, 2009
Posts: 29
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

Joined: Jul 15, 2009
Posts: 537

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]


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
Prannav Santhosh
Greenhorn

Joined: May 09, 2009
Posts: 29
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

Joined: Aug 03, 2008
Posts: 9302
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Pass by value