Re: pass by reference
- From: "Peter Duniho" <NpOeStPeAdM@xxxxxxxxxxxxxxxx>
- Date: Sat, 05 Apr 2008 20:49:07 -0700
On Sat, 05 Apr 2008 16:35:57 -0700, Arved Sandstrom <asandstrom@xxxxxxxxxxxxx> wrote:
However, I don't think I mean "pass by reference" and "pass by value" as
anything other than the intuitive meanings:
Your intuition seems to be broken. :)
a) pass by value - the method sees a copy of the parameter, whatever that
may be;
This is the only thing that Java does.
b) pass by reference - the method sees an alias to the parameter. If the
method uses the parameter it is using the actual thing referred to.
Java doesn't support this.
With objects Java does (b). I don't care if you call it "passing the
reference by value", or "passing a pointer", or whatever. If I do
With objects, Java still does a). The parameter is a variable that references the object. It's not the object itself. And a copy of that reference the variable holds is passed. If Java did support pass-by-reference, and you passed an object reference "by reference", then reassigning the reference would not change the original object, but rather would change the variable that was pointing to (referencing) that object..
It's somewhat difficult to have this conversation in the context of Java, since Java doesn't do "by-reference" parameter passing. However, C# does and Jon Skeet wrote a pretty good article describing the business in that context:
http://www.yoda.arachsys.com/csharp/parameters.html
The main difference between Java and C# in this situation is that C# does in fact support pass by reference. Both languages have reference types (in fact, in Java almost everything is a reference type and you can't make your own value types), and in both languages parameters are passed by value by default, including reference parameters. In C#, you can additionally pass parameters by reference, including references.
If you insist on saying that passing a reference to an object is "passing by reference", then what does it mean to pass a reference by reference, as you can do in C# (and C++ for that matter)?
Some Object so = new SomeObject(...);
makeChangesToFields(so);
then the fields in 'so' are changed outside the method. This is
pass-by-reference behaviour, not pass-by-value behaviour.
No, it's not. You are passing a copy of the variable "so" to the method, and that copy is passed by value. It does happen that the value passed is a reference, but there is no way for the method to change the original variable used as a parameter, and thus it is not being passed by reference.
Don't confuse references to objects with references to parameters. They aren't the same.
For the record I don't like the "swap" example that gets trotted out to
explain that Java is not pass-by-reference (as an example,
http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html).
It's disingenuous. If I write the same example using C++ with pointers to
objects, I get the same results...surprise, surprise.
That's only if you pass those pointers by value. That is, you pass the values themselves rather than a reference to the variables that reference the values. And yes, in C++ just as passing by value prevents you from changing the original variables, so too does passing by value in Java.
In fact, that's exactly why the "swap" example is so simple and applicable. Passing a pointer to an object in C++ is just like passing a reference in Java. In both cases, the reference to the object is passed by value: a copy of the reference is passed, and thus the variable that was used as the parameter is not changeable by the function being called.. So, if you "write the same example using C++ with pointers to objects", you get the same results because in C++ you're passing the pointer to the objects by value, not by reference.
Pete
.
- Follow-Ups:
- Re: pass by reference
- From: Andreas Leitgeb
- Re: pass by reference
- References:
- pass by reference
- From: angelochen960@xxxxxxxxx
- Re: pass by reference
- From: Chase Preuninger
- Re: pass by reference
- From: Andreas Leitgeb
- Re: pass by reference
- From: Lew
- Re: pass by reference
- From: Arved Sandstrom
- Re: pass by reference
- From: Roedy Green
- Re: pass by reference
- From: Arved Sandstrom
- pass by reference
- Prev by Date: Re: java.util.Properties extending from HashMap<Object, Object> instead of HashMap<String, String>
- Next by Date: Re: Java I/O Project
- Previous by thread: Re: pass by reference
- Next by thread: Re: pass by reference
- Index(es):
Relevant Pages
|