Paul Clapham wrote:Well, yes, that code functions just like Java -- and if you wrote that code in Java it would do exactly the same thing. But that's because it doesn't test whether pass-by-reference is happening at all. You never try to change the reference from inside the method. Here's some code which does test that:
In a pass-by-value language you would see "unchanged", but in a pass-by-reference language you would see "something else".
Perry Terrance wrote: because oneObject becomes "aliased" to anotherObject by the "=" sign.
There is no copying of another reference data at all
In contrast to a strict pass-by-value language paradigm (such as everything in Java):
oneObject = anotherObject - would make of a copy of the reference anotherObject is pointing to, and assign it to oneObject - so in total there are now a total count of two references in this scope.
Am I getting all this right?
Bear Bibeault wrote:
Perry Terrance wrote:
In contrast to a strict pass-by-value language paradigm (such as everything in Java):
oneObject = anotherObject - would make of a copy of the reference anotherObject is pointing to, and assign it to oneObject - so in total there are now a total count of two references in this scope.
Absolutely incorrect. No such thing happens in Java. The reference is simply copied and there still exists only a single object that both references are pointing to.
http://www.javaranch.com/campfire/StoryPassBy.jsp wrote:SO... what about Reference Variables (remote controls)? How does THAT work?
Not so tricky, in fact the rule is the same.
References do the same thing. You get a copy of the reference.
So if I say:
Cat A = new Cat();
Cat B = A;
The remote control in A is copied. Not the object it refers to.
You've still got just one Cat object.
But now you have two different references (remote controls) controlling the same Cat object.
http://www.javaranch.com/campfire/StoryPassBy.jsp wrote:So if a Java variable is a cup, with a value in it, what does it mean to "pass" a variable to a method? And are primitives and references treated the same way?
We'll get there, but first let's start with simple assignment.
What does it mean to say:
1) int x = 3;
2) int y = x;
In line 1, a cup called x, of size int, is created and given the value 3.
In line 2, a cup called y, of size int, is created and given the value... 3.
The x variable is not affected!
Java COPIES the value of x (which is 3) and puts that COPY into y.
This is PASS-BY-VALUE. Which you can think of as PASS-BY-COPY. The value is copied, and that's what gets shoved into the new cup. You don't stuff one cup into another one.
Saying int y = x does NOT mean "put the x cup into y". It means "copy the value inside x and put that copy into y".
http://javadude.com/articles/passbyvalue.htm wrote:
Pass-by-value
The actual parameter (or argument expression) is fully evaluated and the resulting value is copied into a location being used to hold the formal parameter's value during method/function execution. That location is typically a chunk of memory on the runtime stack for the application (which is how Java handles it), but other languages could choose parameter storage differently.
Pass-by-reference
The formal parameter merely acts as an alias for the actual parameter. Anytime the method/function uses the formal parameter (for reading or writing), it is actually using the actual parameter.
Perry Terrance wrote:But I think we're talking about the same thing here. Note: I said two references - not objects, so there does exist two references.
Bear Bibeault wrote:
The exact same thing happens in Java as in JavaScript. And the behavior of assignments has nothing at all to do with pass-by semantics.
In a pass-by-value language you would see "unchanged", but in a pass-by-reference language you would see "something else".
Bear Bibeault wrote:Paul's and my examples work the same as in Java; the reference is passed "by value". Whether you think that means pass-by-value or pass-by-reference comes down to what your definition of "pass by reference" means.
Search the Java forums for past discussions on this. I'm not going to rehash it all over.
Pass-by-value
The actual parameter (or argument expression) is fully evaluated and the resulting value is copied into a location being used to hold the formal parameter's value during method/function execution. That location is typically a chunk of memory on the runtime stack for the application (which is how Java handles it), but other languages could choose parameter storage differently.
http://snook.ca/archives/javascript/javascript_pass/ wrote:When passing in a primitive type variable like a string or a number, the value is passed in by value. This means that any changes to that variable while in the function are completely separate from anything that happens outside the function.
Passing in an object, however, passes it in by reference.
| Consider Paul's rocket mass heater. |