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?
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.