aspose file tools*
The moose likes Beginning Java and the fly likes Difference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Difference " Watch "Difference " New topic
Author

Difference

Saravanan Jothimani
Greenhorn

Joined: Feb 25, 2005
Posts: 26
What is the difference between Call by value and Call by reference and what are their uses. Please explain me.

Thanks in Advance
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Has been discussed myriad times. Do a search, or try this link in foldoc.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11230
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Saravanan Jothimani
Greenhorn

Joined: Feb 25, 2005
Posts: 26
Thanks
Saravanan Jothimani
Greenhorn

Joined: Feb 25, 2005
Posts: 26
Can you pls give an example for this
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

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 ]

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
 
jQuery in Action, 2nd edition
 
subject: Difference