Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes equals & hashcode doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "equals & hashcode doubt" Watch "equals & hashcode doubt" New topic
Author

equals & hashcode doubt

dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383


Output is true,false. That is the expected output.

What is the effect of return false in equals method & hash code return 0? I have seen there is no different in execution of code. Can anyone please explain?


SCJP-1.5<br />SCWCD-1.4
Raef Kandeel
Ranch Hand

Joined: Aug 05, 2007
Posts: 87
Remember that you have overriden the equals and the hashcode methods for the class containing the main method. However, you did not really override the hashcode and the equals method for the String class. Just as side note, you cannot override the equals and the hashcode methods for the String class because it is marked final.


SCJP, SCWCD
Chandramouli Ram
Ranch Hand

Joined: Mar 07, 2005
Posts: 65
Hi,

In the code you provided, the equals and hashCode method are not invoked as the Set you constructed is composed using String objects. Hence, only the String object's equals, hashCode methods would be invoked.

To illustrate the effect of your local equals and hashCode, you may create instances of the class that holds this code and then replace Strings everywhere in your code with this new class. You may optionally add print statements in equals and hashCode to verify that these methods are indeed invoked.


Thanks & Regards,<br />Chandramouli Ram
 
wood burning stoves
 
subject: equals & hashcode doubt
 
Similar Threads
adding objects with same hashcodes and true value return by equals() method to a set.
Why is this HashSet allowing duplicates?
Question on HashSet
doubt in hashcode...
why CompareTo exists when we already have equals method