views:

55

answers:

6
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.

+3  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.

Nikita Rybak
Oh ok nice, so thats how I'd break it down then. Now when you have in the comments r1 - Louie1, that would be read as r1 points to Louie1 correct?
fprime
@fprime Yes, 'points' is a good word here. I.e., `System.out.print(r1==r3);` should print `true`.
Nikita Rybak
A: 

You are compare two different Robot objects. Use equals that must be redefined in Robot class.

Stas
+1  A: 

Trace the logic Through

r1 = Huey
r2 = Louie#1
r3 = Louie#2

r1 = Louie#1
r2 = Louie#2
r3 = Huey

does r1 == r2? r1 is Louie#1, r2 is Louie#2

bwawok
+1  A: 

You can visualize your snippet as:

Initial        r1 -> Obj1, r2 -> Obj2, r3 -> Obj3

After r1=r2    r1 -> Obj2, r2 -> Obj2, r3 -> Obj3 ( Obj1 ready for GC)

After r2=r3    r1 -> Obj2, r2 -> Obj3, r3 -> Obj3

After r3=r1    r1 -> Obj2, r2 -> Obj3, r3 -> Obj2

Clearly at the end r1 and r2 do not refer to the same object hence comparing them gives false.

codaddict
+1  A: 

Nope, variables pointing to objects are references. When r1=r2 is executed r1 is pointing the same object r2 was pointing to and after r2=r3 then r2 is pointing the same object r3 was pointing to. So you are comparing the second Robot object´s memory address with the third Robot object´s memory address, and they are different. If you want semantic equalilty your Robot class must override equals() and hashCode(), take a look at: http://www.technofundo.com/tech/java/equalhash.html

xboard
+1  A: 

In r1=new Robot("Huey",2,3); line memory manager takes some memory from heap (for example it is M1 memory) and writes there Robot("Huey",2,3). And r1 refers to M1.

In r2=new Robot("Louie",5,4); line memory manager takes some memory from heap (for example it is M2 memory) and writes there Robot("Louie",5,4). And r2 refers to M2.

And finally in r3=new Robot("Louie",5,4); line memory manager takes some memory from heap (for example it is M3 memory) and writes there Robot("Louie",5,4). And r3 refers to M3.

After r1=r2 command r1 refers to M2. After r2=r3 command r2 refers to M3. And after r3=r1 command r2 refers to M2.

And when you write System.out.print(r1==r2) it tries to compare what does r1 and r2 refer, it means M2 and M3. That's why it returns false.