4 ms·
Weird. It's pass by ref for objects and pass by value for primitives. Why the flame war?
by fleitz 16y ago
Weird. It's pass by ref for objects and pass by value for primitives. Why the flame war?
- randrews 16y agoIt's a war over semantics. Everyone agrees that primitives are pass-by-value, but when you pass an object, is that pass-by-reference because you're passing a reference to the object, or pass-by-value because you're passing the reference by value? It's like the Java equivalent of airplane-on-a-treadmill.
- panic 16y agoThere isn't any semantic ambiguity. The phrase "pass by reference" has a specific meaning. Java does not support pass by reference. Here's a quick test to see if your language uses pass by reference (when translated into Java, it will output "pass by value"): 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') }
- randrews 16y agoYes, you're right, but the argument is still about semantics. Java programmers don't think in terms of passing a reference, they think in terms of passing an object (which happens to be done by passing a reference to it). 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.
- nopassrecover 16y agoAssumes equivalence is referential equivalence, and not overwritten. Of note, in some languages where there are immutable strings (e.g. java I believe) there is only "one true string" for each string (of course this isn't directly relevant to the example code given above). This means "var a = "test"; var b ="test"; assert(a==b);" holds true for referential equality.
- fleitz 16y agoWhich depends highly on the semantics of the == and = operators. 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') }
- deleted 16y ago[deleted]
- baddox 16y agoIt's a "war over semantics" only insofar as is my claim that "a dog is a plant." Using the standard, well-accepted definitions of terms in programming language jargon, Java is pass by value. You can make the semantic argument that the terms "pass by value," "pass by reference," etc. have meanings other than the standard, well-accepted ones, and indeed that would be a "war over semantics," but what's the point? Similarly, I could argue that the definition of "plant" is "something with four legs," but what's the point?
- m_myers 16y agoThat's just the thing: It's never pass by ref. References are passed by value. 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.
- Monkeyget 16y agoIt's not. In java the object's reference is passed by value. 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 http://javadude.com/articles/passbyvalue.htm
- NickPollard 16y agoSurely then, passing an object by reference is just passing the reference to the reference to the object by value? 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
- 16s 16y agoPrimitives are cheap to copy, objects may not be. 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.