This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes hashcode and equals() Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "hashcode and equals()" Watch "hashcode and equals()" New topic
Author

hashcode and equals()

Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 465


My Understading
----------------
this.hashCode() == o.hashCode()----->Both called Objective2425.hashcode().inside the overidden method,super.hashcode calls the Object.hashcode(retrurning one distinct value for this.hashcode and returning one distinct valus for o.hashcode.so two distinct number comparision of ==false,finally the execution of equals() returns false.

Questions
----------
1.//m.equals(mn) is true if and only if mn.equals(m) is true.(where is the use of rule no 2 of equals() contract).FALSE IS RETUERNING AFTER EXCEUTION OF OBJECTive2425.EQULAS().but objective2425.equlas() means look for same object and same values.but both are here in m and mn.
2.when m.equals(mn at line 3 is false,the hashcode contract is no requirements.right?but when i execute statemnts (m.hashcode() and mn.hashcode()),both ARE ALWAYS RETURNING FALSE.
3.does this program of hashcode() and equals() are correctly implemented.If so,how do i know that when executing coding.

Please help me on the above three questions of mine.
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 465
can anyone please answer this.i would be thankful.
Aniket Patil
Ranch Hand

Joined: May 02, 2006
Posts: 218
FALSE WHY IT COMES AS FALSE ALWAYS AND FINALLY THE EQULAS() IS FALSE.


You need to override hashCode() yourself, since the hashCode() of Object returns a different value even for "meaningfully same" objects.

Test it through this:

.//m.equals(mn) is true if and only if mn.equals(m) is true.(where is the use of rule no 2 of equals() contract).FALSE IS RETUERNING AFTER EXCEUTION OF OBJECTive2425.EQULAS().but objective2425.equlas() means look for same object and same values.but both are here in m and mn.

Please note that the equals contract is for YOU to implement when you create your own classes. How do you expect the Java compiler to be intelligent enough to figure out what "equal" means for your classes?
Test this:


For API classes, the equals() method is already overridden to folllow the "equals" contract (This doesent apply to StringBuffer equals()).

when m.equals(mn at line 3 is false,the hashcode contract is no requirements.right?but when i execute statemnts (m.hashcode() and mn.hashcode()),both ARE ALWAYS RETURNING FALSE.

If 2 objects are meaningfully equal, their hashcode MUST be same. Of course, you may choose not to override hashCode() and have the hashcode different for 2 equal objects, but then you are violating the hashCode() contract.
2 different objects MAY return the same hashcode, or it could be different.

does this program of hashcode() and equals() are correctly implemented

Obviously not!

And finally, Patience is a Virtue
[ January 18, 2007: Message edited by: Aniket Patil ]

SCJP 5.0 | SCWCD 1.4 <br /> <br />If you don't know where you are going, any road will take you there!
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 465
Thank you so much for taking the time and sharing your knowledge with me Aniket.I really appreciate it.One more doubt......



objects given are
------------------
Objective2425 mn=new Objective2425(5, "priyua");
Objective2425 m=new Objective2425(5, "priyua");

In that program,.hashcode is implemented correct and equals() is implemented incorrect since the equlas() is retruning false,there is no hashcode requirement.Am i right?
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 465
Hello any answer please
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16695
    
  19

In that program,.hashcode is implemented correct and equals() is implemented incorrect since the equlas() is retruning false,there is no hashcode requirement.Am i right?



Whether the equals() and hashcode() method is implemented correctly or not is ... your call. You are the designer of the class and it is your decision on whether two instances of your class, are equal or not. And if they are equal, then the hashcode() method must return the same hashcode.

Your hashcode() method simply calls the Object class' hashcode() method, which returns a different hashcode for each instance. Is this what you want?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: hashcode and equals()
 
Similar Threads
Correct implementation?? :confused:
Doubt on a K & B LearnKey CD question
equals() method
SCJP1.4 : Confusion in equals() method
hashcode question