Some people will say incorrectly that objects are passed "by reference." In programming language design, the term pass by reference properly means that when an argument is passed to a function, the invoked function gets a reference to the original value, not a copy of its value. If the function modifies its parameter, the value in the calling code will be changed because the argument and parameter use the same slot in memory. The Java programming language does not pass objects by reference; it passes object references by value. Because two copies of the same reference refer to the same actual object, changes made through one reference variable are visible through the other. There is exactly one parameter passing mode -- pass by value -- and that helps keep things simple.
Pass by Value
In Java methods, arguments are passed by value.
When invoked, the method receives the value of the variable passed in. When the argument is of primitive type, pass-by-value means that the method cannot change its value. When the argument is of reference type, pass-by-value means that the method cannot change the object reference, but can invoke the object's methods
and modify the accessible variables within the object.
This is often the source of confusion--a rogrammer writes a method that attempts to modify the value of one its arguments and the method doesn't work as expected. Let's look at such method and then investigate how to change it so that it does what the programmer originally intended.
Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Originally posted by Gregg Bolinger:
[B]But how about this:
Now tell me if that is passed by reference or passed by value. It is not a primitive.
[/B]
Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Oh, I think most of the participants in this thread know extremely well what is going on. They are just using different words and perspectives to describe the same thing - we're knee-deep into mere semantics and arguing whose words are better, with some pretty absolutist statements having been made already. It looks like a technical discussion, but it isn't. It's about words.Originally posted by Gregg Bolinger:
OK, so now I have heard both answers. And so appearntly no one REALLY knows what is going on.
Sigh; I guess your confusion is unavoidable. Gregg, despite appearances we are not disagreeing about what is happening on a technical level. We are discussing how we ought to be talking about it.And no one can show me a certified link on java.sun.com that just says it one way or another in plain english.
Originally posted by Chris Perkins:
I hope it has now been made clear that this does not happen - even the advocates of the "pass by value" terminology have stated that it is only the reference, and never the object itself, that is copied.
Who's redefining things now? I never said this. But, "Reference mode can be emulated by value mode if the language has a reference operator [...] which produces a pointer to an expression with an L-value, and a dereferencing operator [...]" (Finkel, Advanced Programming Language Design). It is this equivalence which supports both viewpoints. If applied consistently, of course.Originally posted by Roseanne Zhang:
Seriously, pass-by-value and pass-by-reference have their different definition in programming language theories. They are not the same thing.
Mmmmm. Don't you think that's a rude and derogative way to finish off a discussion? I'll respectfully decline if you don't mind.You'd better keep your view on this issue in your office or cube, since it does not hurt anything. However, don't spread it, please! Of course, this is only my suggestion.
Originally posted by Roseanne Zhang:
CL
You are correct on Java, everything is passed-by-value.
However, you are not correct on C.
<pre>void func(int **value); // compile in both c/c++</pre>
This is still passed-by-value, the value of a pointer of pointer of an int. If you dereference it inside the function, you will never get the same address as you dereference it from outside, since it is a copy inside. Try it, you will see what I meant. C++ allows passed-by-reference
<pre>void func(int &(*value)); // only compile in c++</pre>
Try it again, please.
Doody calls. I would really rather that it didn't. Comfort me wise and sterile tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
|