• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Pass by value/ Pass by reference

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rex,

Follow the link
Pass By Value or Pass By Reference
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also refer to the following thread for an explanation given by Fred Rosenberger w.r.to reference passing.

https://coderanch.com/t/262603/java-programmer-SCJP/certification/Garbage-collection-objects
 
Ranch Hand
Posts: 37
IntelliJ IDE Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic