News for hackers. Stuff that matters.
Re:Python, as Reviewed by a C++ Programmer by Al-Khwarizmi on Sunday April 23, 2017 @01:24PM (#14179149)
In Java, this code will print the string representation of Lassie:

  Dog toby = new Dog("Toby");
  
  public static void replaceDogWithToby ( Dog d )
  {
    d = toby;
  }
  
  public static void main ( String[] args )
  {
    Dog d = new Dog("Lassie");
    replaceDogWithToby(d);
    System.out.println(d);
  }
In Pascal, this will print Toby:

  var
    toby:tDog;
  
  procedure replaceDogWithToby ( var d:tDog );
  begin
    d := toby;
  end;

  var
    lassie:tDog;
  begin
    {code to initialize toby and lassie here}
    d := lassie;
    replaceDogWithToby(d);
    writeln(getDogName(d));
  end.
The first is call by object (or call by sharing, in my book). The second is call by reference. The behavior is totally different.
Re:Python, as Reviewed by a C++ Programmer by markbnj on Sunday April 23, 2017 @01:44PM (#14179272)
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."
Re:Python, as Reviewed by a C++ Programmer by E6300 on Sunday April 23, 2017 @02:01PM (#14179393)
You 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()).