4 ms·
Passing an object by reference is the same thing as passing an object reference by value. It is just semantics. Its not even splitting hairs. Its demanding that
by binaryfinery 16y ago
Passing an object by reference is the same thing as passing an object reference by value. It is just semantics. Its not even splitting hairs. Its demanding that his specific definitions are somehow more correct than any other.
- okmjuhb 16y agoThis is not at all correct because passing by reference and value have defined and distinct meanings. Consider the difference between C++, which has actual pass by reference: void swap(int& x, int& y) { int t = x; x = y; y = t; }, and Java: void swap(Integer x, Integer y) { Integer t = x, x = y; y = t; }. The C++ version will result in changes to the variables in the calling function and the Java version will not. The real problem is that Java created needless confusion by calling its pointers references for purely marketing reasons.
- binaryfinery 16y agoNo the real problem is that you fail to understand that Java gets to call these things what ever it likes because language has context. And your example fails to compare apples to apples. In Java Integer is an object, but int is not. You are confusing Java things with C++ things. As you can see, Java is not C++, and so your C++ terms and definitions do not apply here. Thank you for playing. Move along.
- okmjuhb 16y ago"No the real problem is that you fail to understand that Java gets to call these things what ever it likes because language has context." This is silly; "pointer" and "reference" both had meanings before Java came along. If Java called pointers "names" instead of "references" it wouldn't mean that its function call semantics were pass-by-name either. And besides; if your argument is that we should use the Java terminology to describe all the aspects of Java under discussion, the fact that the all the Java designers make explicit that the function call semantics pass references as values, not objects as values. "And your example fails to compare apples to apples. In Java Integer is an object, but int is not. You are confusing Java things with C++ things." The argument applies if you use int in place of Integer just the same; or if you make a C++ Integer wrapper class analogous to the Java one. Any comparison here will of course be apples to oranges because C++ supports pass by reference and Java does not. The swap function is impossible to write in Java because it requires allowing called functions to change the values viewed in the caller, which is almost the definition of passing arguments by value. "As you can see, Java is not C++, and so your C++ terms and definitions do not apply here. Thank you for playing. Move along." Leaving aside the childish and insulting tone, the definition of "pass by reference" has nothing to do with C++; if the code were in perl it would still be pass by reference, because "pass by reference" has a meaning that exists outside of any particular programming language and describes a concept. Java function calls are not part of that concept; "there is exactly one parameter passing mode in Java - pass by value".
- binaryfinery 16y agoIn which case there is no such thing as passing by reference. Ever. In C++ when you "pass by reference", what actually happens is that the address of the thing is taken, and a pointer is generated, and then that pointer is passed by value. Do you see? Any argument you put forth to the contrary will involve language specific features and conventions of C++. Your will have to claim that because the C++ compiler does this for you, that makes it special. In Java there are primitives and objects. Primitives are only passed by value. Objects are passed by reference. If you pass an object as a parameter, and then modify a property of the parameter, then it is the original object that is modified. This, in fact, is the same as when you pass an Object in C++ by reference. In contrast, in C++ you can also pass an object by value: the entire object is copied onto the stack. class Foo; void modify( Foo& x) { x.setBar(1);} ; <- pass by reference in C++. class Foo; void modify( Foo x) { x.setBar(1);} ; <- pass by reference in Java. Java spares you the & because Java only passes by reference. Again, to argue the contrary, simply because it passes an object pointer by value, automatically, is to argue that C++ never passes by reference because it too takes the address automatically. The fact is that Java passes by reference exactly as C++ does. However, even if this were not the case, it would also be completely acceptable for Java to refer to what it does as pass by reference simply because it chooses too. These things are object references. If I pass a java object reference, is it acceptable to say I'm passing by reference? If Java people want to say it is, then it is. Will the world end, for example, because the word "heap" means two different things in computer science? http://en.wikipedia.org/wiki/Heap http://en.wikipedia.org/wiki/Heap OMG! Who is right? My point is basically summed up here: http://xkcd.com/435/ http://xkcd.com/435/ You are where the physicist is standing. You insist that Java biologists are using your terms wrong while oblivious to the mathematician. You have your abstractions. Java has theirs. Of course, maybe you're the chemist, and I'm the physicist (I still write assembly) and the hardware guys are the mathematicians. Whatever. As for the insulting tone, yes its a character flaw. When someone makes a statement like "I'm really tired of hearing folks (incorrectly) state [whatever]", its insulting. Its especially insulting when its wrong. In an ideal world I'd be able to respond without resorting to responding in kind, but I'm flawed. Sorry. You're still wrong.
- okmjuhb 16y ago
- nostrademons 16y ago(Can't reply to the other subthread, it's apparently tripped HN's "this is too heated" trigger, so I'll explain here.) It's easiest to illustrate this with code: public class ValueHolder { public ValueHolder(int value) { this.value = value; } public int value; public static ValueHolder THREE = new ValueHolder(3); } public class DoSomething extends TestCase { public void passByValue(byvalue ValueHolder value) { value.value = 1; value = new ValueHolder(2); } public void testPassByValue() { ValueHolder myValue = ValueHolder.THREE; passByValue(myValue); assertEquals(ValueHolder.THREE, myValue); assertEquals(3, myValue.value); } public void passByReference(byref ValueHolder value) { value.value = 1; value = new ValueHolder(2); } public void testPassByReference() { ValueHolder myValue = ValueHolder.THREE; passByReference(myValue); assertNotEquals(ValueHolder.THREE, myValue); assertEquals(2, myValue.value); } public void passReferenceByValue(ValueHolder value) { value.value = 1; value = new ValueHolder(2); } public void testPassByValue() { ValueHolder myValue = ValueHolder.THREE; passByValue(myValue); assertEquals(ValueHolder.THREE, myValue); assertEquals(1, myValue.value); } } Syntax is slightly made-up because Java doesn't have true pass-by-value or pass-by-reference for object, only pass-reference-by-value. But the test cases illustrate the expected semantics for each parameter passing mode. In each case, you're passing a mutable object containing a value to the function. In pass-by-value, you pass a completely new copy of the object in, so the mutation doesn't affect the original object at all, and then the reassignment obviously doesn't propagate back to the caller. In pass-by-reference, you pass in a reference, so the mutation changes the object in the caller's scope, and then the assignment reassigns the variable in the caller's scope to the new ValueHolder. It's this last part that pass-reference-by-value can't do: normal Java semantics let you mutate the object passed in, but you can't make the variable in the calling frame actually point to an entirely new object. Make sense?
- binaryfinery 16y agohttp://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=/com.ibm.xlcpp8a.doc/language/ref/cplr233.htm http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/ind... IBM has it wrong too then. In each of the examples provided, they dont "make the variable in the calling frame actually point to an entirely new object" What actually happens is that the object (or value) that is pointed to by the reference is changed. Specifically, if you took the addresses of a and b before the call, and then after the call, you would see that the addresses have not changed. It is the contents that have changed. This is what happens it C++ in all cases. The only difference between C++ and Java is that Java is always pass by reference for objects and always pass by value for primitives. Pass-by-reference does not mean "I can change the variable in the caller to now point to a new object". It means, if I modify the properties of the parameter, it is modifying the same object that the variable references. In contrast, if you do this in C++: class Foo; void bar( Foo x ) { x.value++; } void main() { Foo y(0); bar(y); } You will discover that y's value remains 0. That is pass by value. class Foo; void bar( Foo &x ) { x.value++; } void main() { Foo y(0); bar(y); } This is pass by reference. y.value is now 1. So now if this is Java: class Foo; void bar( Foo x ) { x.value++; } void main() { Foo y(0); bar(y); } Then y.value is now 1 - just like the pass-by-reference case in C++. So either IBM has it wrong, or you have it wrong.