aspose file tools
The moose likes Beginning Java and the fly likes �call by value� vs. �call by reference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of RabbitMQ in Depth this week in the Open Source forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "�call by value� vs. �call by reference"" Watch "�call by value� vs. �call by reference"" New topic

�call by value� vs. �call by reference"

Jessica Lang
Ranch Hand

Joined: Jul 23, 2002
Posts: 61
When is either cases ("call by value" and "call by reference") being used? How does a programmer identify which call method is used?
For example:
String sound = "Woof.";
Fido.bark(sound); // an example of �call by value, where a copy of sound variable (ie., Woof) is passed to the bark method.
How does a �call by reference� look like?
Peter den Haan
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
The short answer is that Java only supports passing parameters by value.
The long answer is this:
Some would say that your codeIs actually an example of a call by reference, since it's a reference to the String object that is passed to the bark() method. Others, like you, say that the variable "sound" is passed by value, and the fact that this variable happens to contain an object reference is of no importance.
Just looking at Java method calls, these two seem equally valid viewpoints -- the "sound" variable contains an object and you're passing it by reference, or it's an object reference that you're passing by value: just use the picture you're most comfortable with.
But when you take the Java language as a whole into account, it becomes clear that the "call by value" picture is the most natural picture. ConsiderIf you'd adopt the idea that "sound" contains an object that is passed by reference, then you'll have a hard time making sense of the line of code above. It looks like an assignment, but it is in fact some kind of aliasing operation and at the end of it "noise" and "sound" are aliases for the same String.
If, on the other hand, you take the second viewpoint that "sound" contains an object reference, then it's clear what happens: the reference in "sound" is copied into "noise", as you would expect in an assignment. But of course although the references are different they both refer to one and the same object.
This is why it's said that Java method calls pass parameters by value, and doesn't support call by reference.
Does that help?
- Peter
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

Here's a thread detailing/debating the difference.

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Jessica Lang
Ranch Hand

Joined: Jul 23, 2002
Posts: 61
Peter, thanks for the lengthy explanation�.;-) I read the debate thread as mentioned by Joel. I am from structural background and found the explanation at debate thread to be difficult (to understand) for a Java beginner like me, especially, when it involves C/C++ codes.
I think in order not to crack my head further�..I will accept the fact for now that Java only supports �call by value� and not �call by reference�. Maybe, as I get a better understanding of Java, I am able to understand the debate discussion.
Thanks again....
I agree. Here's the link:
subject: �call by value� vs. �call by reference"