• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Difference

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the difference between Call by value and Call by reference and what are their uses. Please explain me.

Thanks in Advance
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Has been discussed myriad times. Do a search, or try this link in foldoc.
 
lowercase baba
Posts: 12754
51
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
imagine you have an electronic document. you want somebody to review it, or use the info in it somehow.

you have two options.

you can email a copy to your associate. They can then mark up that document all they want, edit it, delete it, whatever. your version doesn't change, since there are TWO distinct copies.

OR, you can share the file on your hard drive, and tell them where to find it. if they then edit, change, alter or delete it, that gets refelcted in your version (since it's the ONLY version).

the first version is pass by value. you send a copy of the CONTENTS to the method, but you still have your original, which will be untouched.

the second is pass by reference. you send them a reference to the location of the original. any change by anybody to the object is reflected everywhere.
 
Saravanan Jothimani
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
Saravanan Jothimani
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you pls give an example for this
 
Bartender
Posts: 1840
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm...

Fred's example is a good way to think about the difference, but it's not quite the way Java or C++ defines the terms (although, it seems that most everybody else, including quite a few C textbooks, defines the terms that way, hence all of the confusion about pass-by-reference/pass-by-value in Java)

A Text document is a file in the file System.
An Object is an object in the Heap.

You can obtain a reference to the text document through a text editor. A URI is the "identifier" of the text document, and this is what displays in the text editor's "Open Files" menu.
You can obtain a reference to an Object through any number of means. The memory address in the heap is the "identifier" of the object, and this is what is used in the stack. (Note that you can't see this directly...)

You can e-mail a friend the URI of the document, and then he can edit the same document. Your friend can edit the URI to point to a different document if he wishes. Please note that you have not passed the text document. You have passed the reference to the text document by value.
You can pass a reference of the object to a method. The method can then operate on the same object. The method can change the reference to point to a different object if it wishes. Please note that you have not passed the object. You have passed the reference to the object by value.

You can e-mail a friend the document, and the can edit it. But it is a different document, as it has a different URI. You have passed the document by value.
This is not possible in Java, but C++ supports this. You can pass an object on the Stack to a method, and it is a copy of the object that is passed -- There are now two distinct objects on the stack. You have passed the object by value.

You e-mail a friend the document, and he edits it and your copy is also changed at the same time. You have passed the document by reference. E-mail systems do not support this.
Just as in e-mail, this is not possible in Java. However, C++ supports this. You pass the object to a method, and the method can change the reference and do whatever it feels like, and the original object is affected in the same manner.

Example:

Expected output, pass-by-value: "Bonjour, Monde"
Expected output, pass-reference-by-value: "Bonjour, Monde!!!"
Expected output, pass-by-reference: "Hello, World???"
[ March 01, 2005: Message edited by: Joel McNary ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!