One of my professor has given us a few practice questions for an exam, and one of the questions is something like the below (psuedocode):
a.setColor(blue);
b
It's because the fact that in first example, a
and b
are objects so this is what happens in each of the steps:
a <-- is an object
b <-- is an object
a.setColor(blue); <--
a
becomes blueb.setColor(red); <--
b
becomes reda = b; <-- IMPORTANT:: original
a
object is released and available for garbage collection and nowa
holds the reference ofb
object, which meana
andb
are referring the same object now, which isb
.b.setColor(purple); <-- b is purple now. Since a points to b only, a is also purple
Answer: Both a
and b
are purple at this point.