This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes hashCode and equals Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "hashCode and equals" Watch "hashCode and equals" New topic
Author

hashCode and equals

Sherman Hung
Greenhorn

Joined: Dec 15, 2001
Posts: 17
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.
Brian Glodde
Ranch Hand

Joined: Jun 27, 2001
Posts: 171
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:
Joshua Kueck
Ranch Hand

Joined: Mar 14, 2002
Posts: 71
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.
Joshua Kueck
Ranch Hand

Joined: Mar 14, 2002
Posts: 71
andy lau
Ranch Hand

Joined: Apr 08, 2002
Posts: 51
does the hashCoce() return an object referance???
Brian Glodde
Ranch Hand

Joined: Jun 27, 2001
Posts: 171
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hashCode and equals