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); <--
abecomes blueb.setColor(red); <--
bbecomes reda = b; <-- IMPORTANT:: original
aobject is released and available for garbage collection and nowaholds the reference ofbobject, which meanaandbare 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.