File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes hashCode and equals Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "hashCode and equals" Watch "hashCode and equals" New topic
Author

hashCode and equals

Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
Hi, This is from K&B mock exam.

If class Dog follows the equals and hashCode contracts, and if x and y are reference variables
for two Dog objects, which statements are true? (Choose all that apply.)
Correct Answer
C: If x.equals(y) is true, then x == y may be false.
E: If x.equals(y) is false, then (x.hashCode() == y.hashCode()) may be true.

EXPLANATION:

C and E are correct. C is correct because equals() can be less strict than ==. E is correct
because hashCode() can be less strict than equals().

I am not clear on the explanation. Could you please elaborate on this ? I am getting confused.
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

If x.equals(y) is true, then x == y may be false.


Two objects may be equals but may not represent the same object. The == test is made for objects which point to the same reference. Two dogs with the same name and ID for example may be represented by 2 different instances of the Dog class or by the same instance.

If x.equals(y) is false, then (x.hashCode() == y.hashCode()) may be true.


When 2 objects are not equal, their hashCodes can be equal or unequal. There is no contract that governs this.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hashCode and equals
 
Similar Threads
hashcode() and equals()
hashCode Question
Compare..
equals() and ==, doubt
Equals and HasCode