| Java FAQ | ||
| JSP FAQ | ||
| Servlet FAQ | ||
XyzWs Java FAQ:
Why equals() can be true even if it's comparing two different objects?
Printer-friendly version |
Mail this to a friend
|
Advertisement
|
Why equals() can be true even if it's comparing two different objects?This question comes from a SCJP mock question, "What two statements are true about properly overridden hashCode and equals() method?" The the correct answers are "hashCode() can always return the same value, regardless of the object that invoked it" and "equals() can be true even if it?s comparing different objects". The first statement, "hashCode() can always return the same value, regardless of the object that invoked it" is easy to understand. Because the hashCode() method always returns the hash of the instance of the object, the caller of the method doesn't matter. But why "equals() can be true even if it's comparing different objects"?
Every object has an instance method However, every time when you create a Class, you can override the Note, when you override the You may also find this FAQ useful: What are the differences between the '==' operator and the equals() method? |