Q:

Comparing objects memory address, Java

Robot r1,r2,r3;
r1=new Robot("Huey",2,3);
r2=new Robot("Louie",5,4);
r3=new Robot("Louie",5,4);
r1=r2;
r2=r3;
r3=r1;
System.out.print(r1==r2);

So this program prints false, but I thought it would print true. Its asking if the memory address of r1 is the same as r2. Well r1 is set to equal r2, then r2 is changed to r3, but that shouldn't matter, right? It's still r2 we're comparing it to. 

A:

Let's see the situation after each assignment
// r1 - Huey, r2 - Louie1, r3 - Louie2
r1=r2;
// r1 - Louie1, r2 - Louie1, r3 - Louie2
r2=r3;
// r1 - Louie1, r2 - Louie2, r3 - Louie2
r3=r1;
// r1 - Louie1, r2 - Louie2, r3 - Louie1

In the end, r1 is the first 'Louie' instance (former r2) and r2 is the second.
PS I assume I don't need to comment why new Robot("Huey",2,3) == new Robot("Huey",2,3) returns false.

