aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Equals and HasCode Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Equals and HasCode" Watch "Equals and HasCode" New topic
Author

Equals and HasCode

Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
Source: MasterExam



The answer given is C, E.
But A, B are also correct?
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
No, they are not correct Abhi. The way to think about the three relationships (hashCode() values, equals() return, and == return) is:
The most strict (specific) relationship is ==. If == is true, you can be assured that if the implementations of equals and hashCode are correct, equals will return true, and the hashCode values will be the same.
The next relationship in order of strictness is equals. If equals is true, you must have that hashCode values are the same.
Finally, you have hashCode. This is the least strict relationship. Even for completely unrelated objects of the same class, hashCode may return the same values.
Let me know if that makes sense, and try to reason why A and B are false.


All code in my posts, unless a source is explicitly mentioned, is my own.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952


SCJP 6
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Hope Ruben cleared your doubt, Just an example here



For option A: Even though the objects are meaningfully equal, there memory location is not same.
For option B: it says "x==y may be true.". If it is true then it MUST be equal.

Hope i am clear.

SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Balagopal Kannampallil
Ranch Hand

Joined: Oct 18, 2004
Posts: 136

"==" refers to the memory where the objects are located. If both the objects lies on the same location "==" will return true.


SCJP 5.0 and now fighting with ExtJS
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
That makes perfect sense.
But while adding objects to a hash-based collection we say that if you override the equals() method and dont override the hashCode() then the 2 objects will be considered unequal or different.
So I was trying to figure out the answer that way.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Abhi vijay wrote:That makes perfect sense.
But while adding objects to a hash-based collection we say that if you override the equals() method and dont override the hashCode() then the 2 objects will be considered unequal or different.
So I was trying to figure out the answer that way.


Good, Hash based collections first matches hashCode of both objects , if both objects found in same bucket than only it will call equals(Object) method.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Equals and HasCode
 
Similar Threads
equals method...
equals() and ==, doubt
hashCode Question
hashCode and equals
hashcode() and equals()