3 ms·
I haven't done java in a long time so I won't try to debate the specific language level semantics with you (the same is true of pascal though it has been even l
by markbnj 10y ago
I haven't done java in a long time so I won't try to debate the specific language level semantics with you (the same is true of pascal though it has been even longer), however assuming that your examples run and behave as you describe then what is going on in the java example is that 'Dog d' is passed as a reference to a reference, and then the value pointed to by the argument, i.e. the address of "toby," is replaced with the address of "lassie."
- mlvljr 10y agoYou got it wrong -- it starts as Lassie and stays as such after the function call (as "replacement" only affects the local (stack) copy of the pointer / refernce passed) :) No fancy pointer-to-pointer business thus. No idea what's going on Pascal, though, have to check out myself. upd: upon checking http://wiki.freepascal.org/Variable_parameter http://wiki.freepascal.org/Variable_parameter , I suspect that here a pointer is passed underneath again, but the assignment now copies the bytes over into the pointed-to object.
- E6300 10y agoYou got it backwards. The Java snippet will print "Lassie". In Java, object names are references to objects, and passing them to functions copies the references, so of course modifying the formal parameter (d from replaceDogWithToby()) doesn't modify the actual parameter (d from main()).