News for hackers. Stuff that matters.
Re:Java is Pass-by-Value, Dammit by nostrademons on Friday December 03, 2010 @12:16AM (#1964856)
(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?

Re:Java is Pass-by-Value, Dammit by binaryfinery on Friday December 03, 2010 @01:59PM (#1967025)
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.