-2

I have these two object which have all equal fields.

Why does obj1.equals(obj2) return false?

enter image description here

Johnny Bones
  • 8,786
  • 7
  • 52
  • 117
Elad Benda2
  • 13,852
  • 29
  • 82
  • 157

3 Answers3

2

This is the default behaviour of equals(comparing references). So, if == returns false on Object, .equals() returns false as well. Unless you have overidden equals().

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
2

If you don't override equals method, then when you are doing

obj1.equals(obj2)

It is comparing the values as displayed in your image.

"com.waze.testing.data.Pin@4355".equals("com.waze.testing.data.Pin@4361")

which will returns false as per your result.

vikiiii
  • 9,246
  • 9
  • 49
  • 68
1

Did you override .equals()? If not, it is using Object.equals() which only returns true if they are the exact same instance.

Jesse
  • 3,751
  • 1
  • 21
  • 19
  • if these are two instances of the same class with only int Strings and a float - I don;t think i need to override the equals. no? – Elad Benda2 Jul 03 '14 at 14:17
  • 1
    @user1065869: Yes, you do. The default implementation of `equals()` compares the *references*. So unless they're the same instance of the object, they're not equal. To change that logic, you need to override the method. – David Jul 03 '14 at 14:18