Forums Register Login

About Object

+Pie Number of slices to send: Send
According to Mughal, following 2 statements are wrong. Can anyone explain?
1. If the references 'x' and 'y' denote two different objects, then the expression 'x.equals(y)' is always false.
I think: In some classes 'equals()' is overridden and forced to behave differently as in 'Object' class.
One example is 'String' class, which will check for same sequence of characters, if used as above.
The term 'always' is reason why the statement is wrong.
2. If the references 'x' and 'y' denote two different objects, then the expression 'x.hashcode()==y.hashcode()' is always false.
I think: I've read different things about 'hashcode()' or I've misunderstood it.
According to Mughal, 'hashcode()' will generate a unique hash value for an object.
According to JDK 1.3 documentation, if 'x.equals(y)' evaluates to 'true', then their hash values will be same.
Again, the term 'always' is reason why the statement is wrong.
Still, I need elaborate explanation about hashcode. Any help?
+Pie Number of slices to send: Send
For the question 1) I share the same view as you do.
For question 2)PLease note that if 2 objects are unequal (i.e. obj1.equals(obj2) is false) then, there is no requirement that the hashcodes produced by the 2 objects be the same.
However, producing distinct hascode values for different objects may improve the perfomance of hastbales. This is what the api says.
Hope this helps.
That which doesn't kill us makes us stronger. I think a piece of pie wouldn't kill me. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 659 times.
Similar Threads
x.hasCode() == y.hashCode()
== and equal
Question 10.2 of khalid mughal's book
hashCode and equals
Khalid question on .equals()
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 04:20:17.