Noah Roberts said:
How about a less complex example...
A a = new A();
A a2 = a;
Are you saying all of those a's are different in java? It has been
quite a while since I last used it but it seems to me that they are all
the same instance. That would make C++ references and Java references
pretty close to the same thing.
I'm going to avoid using the term "reference" and "pointer" in my
explanation here of Java's behaviour, and use a slightly different example
which will hopefully be more clear:
Integer a1 = new Integer(5);
Integer a2 = a1;
"a1", and "a2" are two different local variables which point to some
instance of Integer. They are both pointing to the same instance of Integer.
If, later on, you assign a different instance of Integer to "a1", "a2" will
still point to the old instance of Integer. So, for example, if you add the
following line:
a1 = new Integer(42);
then "a1" will now point to an Integer object which conceptually represents
the platonic concept of the number 42, while "a2" will still point to the
old Integer object which conceptually represents the platonic concept of the
number 5.
My understanding is that in C++, if you changed "a1" in that manner, and
if "a2" were a reference, then "a2" would also be updated to point to 42,
and not to 5.
- Oliver