my dog learned polymorphism*
The moose likes Beginning Java and the fly likes HashCode 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 » Java » Beginning Java
Bookmark "HashCode" Watch "HashCode" New topic
Author

HashCode

nirjari patel
Ranch Hand

Joined: Apr 23, 2009
Posts: 371
When an object is created in a java class, does it carry system generated hashcode ? If so, then every object will carry unique hashcode, right ?


In this example, a1 and a2 will have unique hashodes for themselves, which will be different from each other. So if I compare aiHashcode and a2Hashcode, it should return false. So how can I make sure that a1Hashcode and a2Hashcode have same values , since they are system generated values ?
If I override hashCode() to return same hashCode for two objects, then it will return same hashCode even when object are not equal.

Please explain in some detail, rather than pointing to some link. I have done reading and then I am posting here.

Thanks
dileep keely
Ranch Hand

Joined: Jun 28, 2010
Posts: 91
So how can I make sure that a1Hashcode and a2Hashcode have same values ?

override the hashCode() method in your A.java file and return same value.Although not recommended.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

nirjari patel wrote:
In this example, a1 and a2 will have unique hashodes for themselves, which will be different from each other. So if I compare aiHashcode and a2Hashcode, it should return false. So how can I make sure that a1Hashcode and a2Hashcode have same values , since they are system generated values ?
If I override hashCode() to return same hashCode for two objects, then it will return same hashCode even when object are not equal.

There are few rules which one has to know regarding overriding hashCode(). The few which I could recall are-
  • If- instance1.equals(instance2) is TRUE then instance1.hashCode() == instance2.hashCode() should be TRUE.
  • If- instance1.equals(instance2) is FALSE then instance1.hashCode() == instance2.hashCode() can be TRUE or FALSE but ideally it should be FALSE as it improves the performance of the hashtables.
  • Repeated calls for the hashCode on the same instance should return the same value each time provided the state of the instance(those fields used in the equals() method) doesn't change

  • And while calculating the hashCode its better to use the same fields as used in the equals().

    nirjari patel wrote:
    Please explain in some detail, rather than pointing to some link. I have done reading and then I am posting here.

    Thanks


    From what sources/books have you referred to understand this?
    Update: You might want to check this and this to further clear your doubts.


    Mohamed Sanaulla | My Blog
    Jesper de Jong
    Java Cowboy
    Saloon Keeper

    Joined: Aug 16, 2005
    Posts: 14074
        
      16

    nirjari patel wrote:If so, then every object will carry unique hashcode, right ?

    No, you should never assume that every object has a unique hash code. A hash code is not a unique identifier of an object.

    It's easy to see why that's not possible. A hash code is a 32-bit integer. That means that there are 2^32 = 4,294,967,296 possible hash codes.

    Now, let's look at strings of exactly 10 letters (A-Z). There are 26^10 = 141,167,095,653,376 possible combinations of 10 letters. That's much more than the number of possible hash codes. So there must be lots of different 10-letter strings that have the same hash code.

    The most important thing to remember is that when two objects are equal (the equals() method returns true for those two objects), then the hash code of those objects must be the same.

    Note that this works only in one direction: the converse (hashcodes are equal => objects are equal) does not hold. It is allowed for two objects to be not equal, but have the same hash code.

    Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
    Scala Notes - My blog about Scala
     
    Don't get me started about those stupid light bulbs.
     
    subject: HashCode
     
    Similar Threads
    creating a deadlock.
    Identical Arrays
    == operator....
    Array Problem
    hashCode()