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 variables in hashCode( ) and equals( ) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "variables in hashCode( ) and equals( )" Watch "variables in hashCode( ) and equals( )" New topic
Author

variables in hashCode( ) and equals( )

David Jones
Greenhorn

Joined: Dec 26, 2003
Posts: 27
Hi all,
If variable a, b is in the equals() method, which of the following variables should also exist in hashCode() method?
1) a and b
2) a or b
Should both a and b must exist in hashCode() or, just use one of them is OK?
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
Originally posted by David Jones:
Hi all,
If variable a, b is in the equals() method, which of the following variables should also exist in hashCode() method?
1) a and b
2) a or b
Should both a and b must exist in hashCode() or, just use one of them is OK?

None. The contract of hashCode() in general and its mutual with equals() part in particular specifies that:
  • if two objects are meaningfully equal, calling hashCode() on both should produce the same result
  • if two objects are not equal, hashCode() doesn't have to return a different result

  • The implications are as follows:
    --You can return the same integer from the hashCode() on unequal objects
    --If the hashCode() returns different values, the two objects cannot be equal.
    As you see, there's nothing here to stipulate the usage of instance variables used in equals(). So, what's all this question buzz about? Apparently, it's about efficency of the hash code implementation - the more unique hash code an object yields, the better is the hashing storage/search algorithm. The uniqueness is achieved through applying bit wise operators to the fields that may very well be used in equals() body. Tons of bloated door stoppers have been written on hashing, but all we need to know for the exam is in the API. Read Object's equals() and hashCode() documentation. I also found Kathy & Bert's book helpful on that.
    David Jones
    Greenhorn

    Joined: Dec 26, 2003
    Posts: 27
    Thanks for the reply. I ask this question because I saw some questions regarding this topic (Whizlabs). They said that the appropriate implementation of the hashCode() are (based on their answers):
    i) final variable should not use
    ii) insignificant variables (i.e. the variable can be computed from other variables) should not use
    iii) variables in hashCode() should not include any variables which are not included in the equals()
    iv) hashCode() may contain all or subset of the variables used in the equals()
    ...
    I am not sure whether the exam includes this topic...
    Thanks
    dennis zined
    Ranch Hand

    Joined: Mar 07, 2003
    Posts: 330
    Hi David. Just curious, based on Whizlab's explanation what was their correct answer?


    SCJP 1.4<br />SCWCD 1.4
    Vad Fogel
    Ranch Hand

    Joined: Aug 25, 2003
    Posts: 504
    From my experience, this kind of semantics wasn't on the test. I like the way hashCode() implementation is outlined in K&B:
  • Legal hashCode() implementation just compiles
  • Appropriate hashCode() implementation is a legal one that also complies with the equals()-hashCode() contract
  • Efficient hashCode() implementation is an appropriate one that provides for good hashing algorithm

  • For example, the following hashCode() is just legal:
    Although it compiles fine, there's no guarantee that the integer returned will always be consistent in accordance with the contract.
    The following hashCode() is legal and even appropriate:

    It consistently returns the same integer for two meaningfully equal objects (the assumption is that equals() is implemented properly)(should I mention it's badly inefficient?? ).
    Finally, this hashCode() implementation is efficient:

    BTW, using transient variables in equals() or hashCode() is not recommended as it makes their implementations inappropriate.

    [ December 29, 2003: Message edited by: Vad Fogel ]
    David Jones
    Greenhorn

    Joined: Dec 26, 2003
    Posts: 27
    ... and yes, random() should not be used (Whizlabs also mentioned this). And they also said that if array is used in equals(), then you should implement the hashCode() that include all the elements of the array (exclude the null value in the array).
    And here is one of the questions regarding this topic:
    Which of the following statements are true:
    a) if a and b are two StringBuffer references, and a.hashCode() == b.hashCode() is true, then a.equals(b) will also be true
    b) if a and b are two Byte references and a==b is true, then a.equals(b) will also be true
    c) A class SomeClass has two variables, x and y. The overridden equals() method uses both these variables for the rquals comparison, but the hashCode() method may choose to use only one of them
    d) If a is an Integer reference, and b is a Float reference, then a.equals(b) will result in an exception being thrown at runtime
    e) Only choice A is correct
    The answer is B and C.
    What I am interested is option C. They explain that the hashCode() method may contain all or subset of the variables used in the equals comparison but it should not contain the variables, which are not used for the equals comparison. Since I cannot find such information in K&B book, I am not sure whether this type of question will be appeared in the exam.
    David Jones
    Greenhorn

    Joined: Dec 26, 2003
    Posts: 27
    sorry that for the array, it should the class contain the array variable.
    sanjana narayanan
    Ranch Hand

    Joined: Nov 25, 2003
    Posts: 142
    Hi,
    Yes even Dan had given an explanation on using variables in the calculation of hashcode method.
    It is given in the follo URL. Refer to 8 and 9th questions and ans accordingly. He had mentioned that we should not use the variables in the hashCode implementation, which are not used in the calculation of equals method.
    http://www.danchisholm.net/dec20/topic/section9/hashcodes1ans.html.
    -Snajana
    Jim Yingst
    Wanderer
    Sheriff

    Joined: Jan 30, 2000
    Posts: 18671
    Looking at the original question, I'd say that if a and b are used in the equals() method, you probably should use them in the hashCode() method too - but you're not required to. The interpretation of "should" is somewhat arbitrary here, so don't worry about it too much. The real exam won't try to split this particular semantic point.


    "I'm not back." - Bill Harding, Twister
    David Jones
    Greenhorn

    Joined: Dec 26, 2003
    Posts: 27
    Hi Sanjana,
    I found that the link does not work. Could you please check the link?
    Thanks
    sanjana narayanan
    Ranch Hand

    Joined: Nov 25, 2003
    Posts: 142
    Sorry David.
    You can go the this website
    http://www.danchisholm.net/dec20/topic/
    Go to hashcode under the collection framework section.
    In this section, questions(8,9) have some information relevant to what you were asking.
    -Sanjana
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: variables in hashCode( ) and equals( )
     
    Similar Threads
    Behind HashSet
    Regarding the hashcode
    hashcode and equals