aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes please explain the output? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "please explain the output?" Watch "please explain the output?" New topic
Author

please explain the output?

Sonali Sehgal
Ranch Hand

Joined: Jul 09, 2009
Posts: 75
This is an enthuware question.

Consider the following class:




Which of the following statements are correct regarding the above class?
Option 1: This is an invalid implementation of hashCode() method with respect to the given equals() method.
Option 2: Only one of the Info objects will be stored in the HashMap.
Option 3:Both the objects will be stored in the HashMap however retrieving them using the Info objects as keys will not be possible.
Option 4:An exception will be thrown at run time at line //2.


Please explain what is the right answer for this question???

I am not able to understand this code of hashcode???
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Option 1 should be the correct answer. Please check the other post for explanation.


JDBCSupport - An easy to use, light-weight JDBC framework -
Balraj Momi
Ranch Hand

Joined: Jul 23, 2009
Posts: 45

Question 1 is correct is because hashCode() and equals() method should use same instance variables. And in this question, only one String s1 is being used in hashCode() method. This implementation will not fail, It will still work. But It is not in line with equals method.

Option 2: Wrong - Both object will be stored because both objects are not equal.
Option 3: Wrong - It will be possible to retrieve values from this map by using either of these objects as Key
Option 4: Wrong - No Exception thrown

Regards
Balraj


Regards
Balraj Kumar
SCJP 5 95%
SCWCD 82%
Preparing for SCBCD
shivendra tripathi
Ranch Hand

Joined: Aug 26, 2008
Posts: 263
The equal contract says, if equals() return for two object then hashCode() for these two object must be same. In the current implementation suppose I have two objects with filed values "aa" , "bb", "cc" and "bb", "aa", "cc". For this example equals() will return true but hashCode for these two objects won't be same.
I guess by now you must have got your ans. You can use a thumb rule that in proper hashCode implementation you should use all the information used in your equals() method implementation.


SCJP 1.5(97%) My Blog
Jyoti B.Shah
Greenhorn

Joined: Nov 13, 2008
Posts: 17
Shivendra,

equals method will return false for two objects with filed values "aa" , "bb", "cc" and "bb", "aa", "cc" and hashcode for them will return different values, which is perfectly legal. I am not sure how the implementation of hashcode() method is invalid. It would be great if someone could provide a example where equals() will return true but hascode() for those two objects will return different values.

Output of following code returns -

false
hello
world



SCJP(95%)
Jyoti B.Shah
Greenhorn

Joined: Nov 13, 2008
Posts: 17
Got answer to my question. If we have two info objects as {"aa","bb","cc"} and {"a","abb","cc"}, although they will be equal(s1+s2+s3 is same for both objects) but their hascode will be different which voilates the hascode() contract.
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Watch out with the example you just came up with.

{"aa","bb","cc"}, {"a","abb","cc"} and your implementation of equals means that two objects that are actually meaningfully different will pass the equals test. You should put some sort of separator into the check, like this..


or write different if statements that check for each variable on its own ...
shivendra tripathi
Ranch Hand

Joined: Aug 26, 2008
Posts: 263
Hi Jyothi,

Good catch, I wanted to explain the scenario you mentioned "aa" , "bb", "cc" and "a", "abb", "cc" but somehow i put it in wrong way...
Sonali Sehgal
Ranch Hand

Joined: Jul 09, 2009
Posts: 75
Hi,

Thanks to all of you for explaining me this topic of hashcode I think I am able to understand it better now.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: please explain the output?