aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes hashCode() usage 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() usage" Watch "hashCode() usage" New topic
Author

hashCode() usage

Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Hi, This is from Whizlabs..Could you please suggest usage


OCPJP 6.0-81% | Preparing for OCWCD
http://www.certpal.com/blogs/cert-articles | http://sites.google.com/site/mostlyjava/scwcd |
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3371
    
    9
Stop posting topics and read the contract.

Look at the last two bullets.
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Stephan van Hulst wrote:Stop posting topics and read the contract.

Look at the last two bullets.
Steve, I read the contract. Maybe I am not clear .
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

The hashcode of two objects can be same value, and to further confirm it, you can use the equals() method. But, for two objects, if the hashcode are different, then you can say, both are unequal. This scenario in used in HashMap, etc


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Rohit Ramachandran
Ranch Hand

Joined: Oct 05, 2010
Posts: 102
Stephan stop posting replies that are not relevant to the question. Thank you.
Rohit Ramachandran
Ranch Hand

Joined: Oct 05, 2010
Posts: 102
Dude it's simple.

If a1.hashcode() != a2.hashcode(), then they can never be equal and hence it confirms inequality.

However, if a1.hashcode()== a2. hashcode(), it doesn't confirm that a1 equals a2, only after (a1equalsa2)=true, you know that they are equal.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Please be polite here, and, Rohit, you are mixing == and equals() method. The original post question asked with related to usage of hashCode() and equals() methods. If you follw the contract, then only you can expect the intended behavior.

In HashMaps, it checks the hashCode() of two objects, if both are same, then it go further to check whether the objects are meaning fully equal. If the hashCode() aren't same, it assumes, the objects shouldn't be meaningfully equal.

But, if don't follow the contract, you can't expect the above behavior.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

Rohit Ramachandran wrote:Stephan stop posting replies that are not relevant to the question. Thank you.

Stephen posted a link to the hashCode-equals contract which I think is on topic. Also your tone is against the Be Nice rule at javaranch. Stephan your reply could also have been more nice in reminding Harikrishna to read the contract...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3371
    
    9
I realize, and I apologize to Harikrishna.
Rohit Ramachandran
Ranch Hand

Joined: Oct 05, 2010
Posts: 102
Abimaran, I edited my code, each time I used = i meant equals but yeah my bad.
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Hi Rohit, I am clear with your reply. Back to my original question, object inequality means, are you referring something like this, !(o instanceof Question01) from the following code ?
Rohit Ramachandran wrote:Dude it's simple.

If a1.hashcode() != a2.hashcode(), then they can never be equal and hence it confirms inequality.

However, if a1.hashcode()== a2. hashcode(), it doesn't confirm that a1 equals a2, only after (a1equalsa2)=true, you know that they are equal.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hashCode() usage
 
Similar Threads
hash code problem
Confusion on Hashcode and equal method
Hash code
hashcode and equals