aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Pass by value/ Pass by reference 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/ Pass by reference" Watch "Pass by value/ Pass by reference" New topic
Author

Pass by value/ Pass by reference

Rex Isaacs
Greenhorn

Joined: Jul 26, 2003
Posts: 8
Hi,

I've a question regarding Pass by value/Pass by reference.

Please see the sample I tried.




Here after calling the method callme() with pbv as the object, when I print the value of Price and Value, it prints 300 and 400 instead of 100,200.

My doubt is that if it is pass by value, the value should be 100,200 no? Can anybody explain why ?

Thanks,
Rex
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Rex,

Follow the link
Pass By Value or Pass By Reference


cmbhatt
Meena R. Krishnan
Ranch Hand

Joined: Aug 13, 2006
Posts: 178
You are passing the address and not the actual value. Any changes made to the object at that address is global.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

The value being passed is a copy of the reference.

Within main, the original reference is assigned to the local variable pbv. Within callme, a copy of that reference (i.e., a value) is assigned to the local variable ppbv. However, both of these point to the same object. So either reference can be used to change the object's state, and this change will be "visible" through the other reference.

On the other hand, if you reassigned ppbv to point to a different object...


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Neo Phesus
Ranch Hand

Joined: Feb 22, 2007
Posts: 71
Hi Rex,

Could you please explain why did you think it should be 100 and 200?? and what difference does it make incase of a pass by value??

Regards,


Neo<br />CCNA, SCJP 1.5, SCWCD 1.4
Neo Phesus
Ranch Hand

Joined: Feb 22, 2007
Posts: 71
OOps !! Sorry..!! I did not check the replies
Meena R. Krishnan
Ranch Hand

Joined: Aug 13, 2006
Posts: 178
Also refer to the following thread for an explanation given by Fred Rosenberger w.r.to reference passing.

http://www.coderanch.com/t/262603/java-programmer-SCJP/certification/Garbage-collection-objects
Zhao zhenhua
Ranch Hand

Joined: Jul 17, 2006
Posts: 37

Hello,Rex Isaacs:
Acturally,when you get "PassbyValue pbv = new PassbyValue(100,200);" that create an object and initialize the two variable and set Price=100 and Value=200;but when get "callme()" the reference is the "object",following

void callme(PassbyValue ppbv){
System.out.println(ppbv.getPrice()+ " "+ppbv.getValue()); ppbv.setPrice(300); ppbv.setValue(400); }

Here after calling the method callme() with pbv as the object, the value of Price and Value had been reset, so it prints 300 and 400 instead of 100,200.

If I got any wrong ,please tell me anyone about my answer include my caluse syntax.


Head first Servlet and JSP
Rex Isaacs
Greenhorn

Joined: Jul 26, 2003
Posts: 8
Thanks all for your valuable replies to make me understand the concept.

I got it now.

[ April 30, 2007: Message edited by: Rex Isaacs ]
 
jQuery in Action, 2nd edition
 
subject: Pass by value/ Pass by reference