• 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

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i know that passing values in a method does not affect the original values.
but the reference thing is getting to me i cant seem to grasp this concept.... when r we able to change these values?
HELP
 
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All parameters are passed by value in Java. When a reference type is passed as a parameter, a copy of the reference is passed. Modifying the copy of the reference has the same effect as modifying a copy of a primitive type: none. However, the copy of the reference may be used to call methods or access public fields (if any) of the object represented by the reference, thereby changin its state.
Consider the following example, adapted from the RHE book (see the Bunkhouse), 2nd ed.

Running the program produces the output "Pink." However, if you replace the body of changeLabel() with

the program produces the output "Blue."
Hope this helps.
Craig
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mansoor,
If you have a C/C++ background, it's helps to think of reference as a pointer. In C, pointer is passed by value, meaning, the address itself is passed to the function. The implication of this is that you can change the thing pointed to by this pointer, but not the pointer itself, because it's just a copy.
It's exactly the same as how references are passed around in Java.
Cheers

Originally posted by mansoor iqbal:
i know that passing values in a method does not affect the original values.
but the reference thing is getting to me i cant seem to grasp this concept.... when r we able to change these values?
HELP


 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When a simple type is passed to a method, it is done by use of call-by-value. Objects are passed by use of call-by-reference.
Also run the following sample and will make it simple.

Hope this helps,
Anna S Iyer.

[This message has been edited by Anna s Iyer (edited October 14, 2000).]
 
mansoor iqbal
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank u all for ur help.....!!!
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd like to correct what Anna has mentioned for objects. Both primitive types as well as object references are passed by value (i.e., a copy of the value is sent to the called method). The speciality in case of objects is that the value that is copied is nothing but the reference (memory address) of the object. So, the called method is also pointing to the same object as the calling method. So, whatever changes that the called method makes to the object affects the calling method too.
Hope this is clear.
Srikrish.

[This message has been edited by srikrish (edited October 14, 2000).]
reply
    Bookmark Topic Watch Topic
  • New Topic