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 query 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 query" Watch "hashcode and equals query" New topic
Author

hashcode and equals query

Mili Dua
Greenhorn

Joined: Jul 31, 2012
Posts: 1
Hello everyone. This is my first post in the JavaRanch SCJP forums and the look and the feel of this place is uber best .
I have a query regarding the HASHCODE AND EQUALS CONCEPT and I figure this is the best place to ask -:

[[As per the contract of hash code() --

1. If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.

2. It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.]]


IS THERE ANY POSSIBILITY THAT or i should say that whether it can happen that the two objects are unequal but calling the hashcode() method on each of them must produce the same integer result ??
Gaurangkumar Khalasi
Ranch Hand

Joined: Jun 02, 2012
Posts: 186
Mili Dua wrote:IS THERE ANY POSSIBILITY THAT or i should say that whether it can happen that the two objects are unequal but calling the hashcode() method on each of them must produce the same integer result ??

Yes it is possible...
For example, you can override hashCode() method in your class as following than it will return same integer for all its instances either equal or unequal.

Note: It is not an appropriate way to override but it is a legal override example of hashCode().
Phani Kiran Rudravarapu
Greenhorn

Joined: Jul 31, 2012
Posts: 3

Mili Dua wrote: IS THERE ANY POSSIBILITY THAT or i should say that whether it can happen that the two objects are unequal but calling the hashcode() method on each of them must produce the same integer result ??


Is there a possibility that two objects are unequal but calling hashCode produces the same integer result? Yes, if the hashCode method is not properly overridden.

Are there any scenarios where it should be like this? No. This kind of hashCode implementation is an inefficient one. Legal, but inefficient.

To avoid scenarios like these, the same instance variables used in overridden equals method are used in the overridden hashCode method. And static and transient variables are not supposed to be used.

[OCJP6 -91%] , EJB Novice
 
wood burning stoves
 
subject: hashcode and equals query
 
Similar Threads
hashCode Question
Hash code
hashcode question
Anyone done SCJP 1.4 Beta , please comment~ :)
hashCode and equals