This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Jessica Lang
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
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?
Thanks....
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a thread detailing/debating the difference.
 
Jessica Lang
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic