This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes hashcode() contract Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "hashcode() contract" Watch "hashcode() contract" New topic
Author

hashcode() contract

Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
I have not able to clearly decipher the bold lines below

Whenever it (hascode() ) is invoked on the same object more than once during an execution of a java application, the hascode() method must consistently return the same integer, provided no information used in equals() comparison on the object is modified.


SCJP 6.0 96%

(Connecting the Dots ....)
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

Consider a car. Lets say it has a name and type. The equals and hashCode can be based on the name and type. If the name changes the output of equals may change and thus the output of hashCode may also change.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

That means, the hashCode() and equals() methods should return same values of their own in every invoking of them on a object, such there is no modification in the object's state, which used to determine the hashCode() and equlas() method.

Simply, without changing the object's fields, which are deterministic functions of the hashCode() and equlas() method, these two method should return the same value!

[EDITED]


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Abimaran Kugathasan wrote:That means, the hashCode() and equals() methods should return same values in every invoking of them on a object

hashCode returns int ;where equals return type is boolean
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Seetharaman Venkatasamy wrote:
Abimaran Kugathasan wrote:That means, the hashCode() and equals() methods should return same values in every invoking of them on a object

hashCode returns int ;where equals return type is boolean


I don't mean in that way, rather, I do mean that they should return same value individually. For example, if equals() method return true, the for every invocation of that method, with the no modification on the object state, should return true, and it also apply to hashCode() which returns a int value.

Sorry for that ambiguity. I just correct that post! Thanks Raman
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Abimaran Kugathasan wrote:
Sorry for that ambiguity

no worries, keep rocking
Raza Mohd
Ranch Hand

Joined: Jan 20, 2010
Posts: 247

hi,

See the default implementation of equals()(of Object Class) and Hashcode() (of Object Class) is different to implementation in (say,String ) where they are overridden and implemented in such a way that , equals() consider the character sequence as a benchmark to check equality and in same way HashCode() is implemented which make this Line not get contradicted.

If the two objects are equal, the Hashcode() will return the same integer value for both.


So if you override equals in your class you must need to override hashCode() to stick to above line.

Otherwise HashCode() will work abnormal.


Thanks and Regards,
Mohd. Raza,


Good luck!!
A small leak can sink a Gigantic ship.>
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
@All above

When you change any variable , then object is also not same. The object would be different ....as you have said that equals depends on that instance variable two, and two different objects may have different hashcode values.


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hashcode() contract
 
Similar Threads
HashMap Duplicate Element Problem
Creation of Ineger Wrapper Object
Inquisition qustion regarding equals() and hashCode()
What is use of hashcode?