Why the following statements are false? 1. If the reference x and y denote two different objects, then the expression x.equals(y) is false. 2. If the reference x and y denote two different objects, then the expression (x.hashCoce() == y.hashCode()) is false.
1. They are 2 different objects, therefore 2 different references. Not equal. From the JavaDocs equals() definition: "Indicates whether some other object is *equal to* this one." 2. It is true the hashCode() values are different. Code:
If reference x and y are of type Object, then x.equals(y) is equivalent to x==y. This will compare the references. However, if reference x and y are of some type that Overrides equals (most do), then it usually compares the actual objects to one another.
Andy, Try compiling the code example I posted, it'll write out the hashCode(). Also, the JavaDocs are a good source of this type of information. In short, hashCode() does just what it says, returns a hash value for the object for use in data structures such as a hashtable.