Huge war over whether Java is pass by reference or pass by value.
theserverside.com
theserverside.com
Do these terms mean something different in Java? Again, I'm not a big Java dev, but I am interested in why there's such a big debate on this topic.
In C++, I typically pass-by-reference when I need to change the thing. When I don't need to change the thing (but it's expensive to copy) I pass by const reference so the thing cannot be changed, but is still efficient.
I seldom pass-by-value unless it's a primitive type (int, char, etc.).
From the perspective of an object being passed to a method, Java is pass by reference. No copy of the object is made.
From the perspective of the implementation of methods, Java is pass by value, with the "value" representing any "object" involved being a pointer to that object.
Most of this argument seems to be people choosing one view of this duality and screaming that the other side is stupid and wrong. It's a tedious and annoying thing to see play out (although nowhere near as bad as your average "static vs dynamic typing" debate).
If Java was truly pass-by-reference, then certain things that are not currently possible would be. It would make some existing code pretty silly frankly.
Probably 99% of the reference usage I've seen (all in C++, fwiw) could be about as easily accomplished with pass-by-sharing through pointers. The actual valid uses never seemed worth the nasty copy constructor bugs created by people mistaking reference semantics for pointer semantics.
At this point I gave up on "true" references so long ago that I tend to fall in with everyone else and call pointer-based sharing "pass by reference". I suppose I should amend my initial comment to specify that most of the team "pass by reference" is really "pass by sharing (through pointers)", but I'll wait on that until something convinces me that true references shouldn't go the way of dynamic scoping.
I'd rather they'd went the route of forcing you to always use wrapped primitive types, but it's hard to fault the Java designers on that decision, especially when you consider the context of the time when it was made.
"Java is ALWAYS pass-by-value...", they nod, "...including OBJECT REFERENCES."
"What?"
Sorry for OT.
http://stackoverflow.com/questions/40480/is-java-pass-by-ref...
I think Java causes this confusion because it's so restrictive about what you can do with pointers. In C, the difference between a struct and a pointer to a struct is very clear. In Java, the fact that X x = new X() stores a pointer is sort of invisible.
And people ask why...
It's like the Java equivalent of airplane-on-a-treadmill.
function f(x, y) { x = y }
a = new A()
b = new A()
assert(a != b)
f(a, b)
if (a == b) {
print('pass by reference')
} else {
print('pass by value')
}There's a correct answer to airplane-on-a-treadmill too, but that doesn't mean it's not an argument about semantics. Please let's just stop this here before this thread becomes like that other thread.
So if in java you decided to create an assign method (cuz remember operators are evil in java) and overrode equals then you could create such a situation in which it would return pass by reference.
Plus the equals operator in java is kind of f'ed anyways as it is.
Conversely if the language was pass by value (or not retarded) then code such as this should work.
function f(x, y) { x == y }
a = new String("foo");
b = new String("f").append("oo");
assert(a != b)
if (f(a, b)) {
print('pass by value')
} else {
print('pass by reference')
}If it were pass by ref, you could make a parameter point to a different object and it would still be visible after the method had returned.
There is a difference between passing an object by reference and passing the reference of the object by value. In the first case caller and callee share the same pointer to the same object while in the latter case each have a different pointer to the same object.
This article explains it very well : http://javadude.com/articles/passbyvalue.htm
It's references all the way down!
To take it to a lower level, we're passing a string of binary digits to the function. This happens either through a register, or through the stack. The function then has to decide what to do with it - either it uses it as the value of something, or it takes it as a memory address and derefences it. It can then repeat this last step for as many levels of indirection as it wishes.
It's clearer in C.
void f(struct object a); // Pass struct by value void f_(struct object* a); // Pass struct by reference, pass reference by value void f__(struct object a); // Pass reference to struct by reference, pass reference to reference to struct by value void f________struct object a); // What the hell are you doing? You've created a monstrosity
Edit: I've never used Java that much. My experience with passing things to functions/methods is primarily in C++. When I say primitive types, I mean char or int.
Before downvote: please explain, thank you.
Pass by reference means you change the value of a variable passed from the caller's context. You can't do that in Java.