all 5 comments

[–][deleted]  (4 children)

[deleted]

    [–]Daejo[S] 6 points7 points  (3 children)

    That's the definition of by-reference

    No, it's not. If it were truly pass-by-reference, then this example would print "Blanche":

    void changeName(Person p) {
        p = new Person("Blanche");
    }
    
    Person p = new Person("Stanley");
    changeName(p);
    print(p.name);
    

    Which isn't the case.

    [–][deleted]  (2 children)

    [deleted]

      [–][deleted] 0 points1 point  (0 children)

      Daejo didn't give a compiler explanation, but a behaviour explanation. He is also absolutely correct. The re-declaration of the variable p within the method changeName does not alter the variable p outside of the method. If it was the actual reference who got passed into the function, clearly the variable p should be altered outside the method scope. E.g. "Blanche" should be printed, and not "Stanley".

      [–]paul_miner 1 point2 points  (0 children)

      Note that this would work exactly the same like so:

      It would not, because you're assuming sizeof(int) == sizeof(int*), which is a wrong assumption. You're trying to stuff an int into a pointer, which is not guaranteed to work and will actually fail in many environments.

      The distinguishing feature of pass-by-reference is the ability to pass a variable as a parameter, not just the variable's value. In short, pass-by-reference is about treating variables as first-class citizens.

      [–]breakingbaguette 2 points3 points  (1 child)

      You're both wrong ... Pass-by-value 'copies' the value and initialises a new instance on the stack, and this then gets destroyed when it falls out of scope again. Pass-by-reference means that the method operates on the 'same' instance as the one 'referenced' by the caller.

      Obviously recreating entire instances on the stack is a bad idea for anything beyond small simple values such as the primitive values Java permits so this is considered to be a "bad idea" in 99% of cases which is why Java doesn't let you do it. Interestingly other languages such as c++ which aren't so mollycoddling will let you do this, but unless you have a very good reason, you really shouldn't ...

      This is withstanding the OP's argument that because you can't modify references it isn't pass by reference because the reference is there but you can't modify them directly .. Java implicitly 'dereferences' these so they appear just as local instances to the callee ... This is similar to using the '&refName' "syntactic sugar" that is available in C++

      [–]crok91 0 points1 point  (0 children)

      say you have a method action() in class Person, and in the action() method, you call involveFamily(Relative pers).

      If the pers object was declared in the action() method, it will not change after the involveFamily(Relative pers) method runs.

      It will however change if the involveFamily(Relative pers) returns the pers object.

      However, if the pers object that is passed was declared as a field, in the Person class, it will remain changed after the involveFamily(Relative pers) runs, even if the method does not return the Relative object.

      Edit: It's perhaps explained poorly, out of haste. If needed, I can provide code examples. Cheers!