Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Pass by value/ Pass by reference

 
Rex Isaacs
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rex,

Follow the link
Pass By Value or Pass By Reference
 
Meena R. Krishnan
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are passing the address and not the actual value. Any changes made to the object at that address is global.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Neo Phesus
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Phesus
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OOps !! Sorry..!! I did not check the replies
 
Meena R. Krishnan
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
Firefox Browser IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rex Isaacs
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all for your valuable replies to make me understand the concept.

I got it now.

[ April 30, 2007: Message edited by: Rex Isaacs ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic