File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes hashCode Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "hashCode" Watch "hashCode" New topic
Author

hashCode

anushree ari
Ranch Hand

Joined: Sep 30, 2002
Posts: 98
hi guys,regarding hashcode and equals is this true,
1)The hashCode method defined by class Object does return distinct integers for distinct objects.
2)For two object references referring to the same object, the hashCode method returns a same integer.
waiting the results,
[ December 02, 2002: Message edited by: Marilyn de Queiroz ]

anushree
anushree ari
Ranch Hand

Joined: Sep 30, 2002
Posts: 98
iam sorry it's in the topic of hashcode,
thx
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
The first of your statements is not guaranteed to be true always.
The hashCode method implemented by Object is actually a native method so differences can exist between platforms. Usually, the hashCode method returns the address of the object in memory. However, if the address space of the computer is larger than 32 bits then the complete address can not be returned as a hashCode of type int. If your program is running on a computer with a very large memory space then you might find that unique object instances sometimes have the same hashCode.
[ November 29, 2002: Message edited by: Dan Chisholm ]

Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
anushree ari
Ranch Hand

Joined: Sep 30, 2002
Posts: 98
i didn't get dan,
according to hascodemethod, the equals object must produce equal integers but there is no restriction unequal objects produce different integers,
and the second is == opertor the refernces are equal it return ttrue, but this is not in hashcode right,
pls anybody correct me
thx
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Originally posted by anushree ari:
according to hascodemethod, the equals object must produce equal integers but there is no restriction unequal objects produce different integers...

Yes, that is true. The first thing to remember is that the hashCode contract does not require unique hashCodes for unique Objects. Even so, most people know that the actual implementation of the Object.hashCode method usually returns the address of the Object in memory. Your first statement in the first post of this thread is as follows.

1)The hashCode method defined by class Object does return distinct integers for distinct objects.

My earlier response simply states that the usual behavior of the Object.hashCode method is not guaranteed. There is no guarantee that Object.hashCode will return a unique hashCode for unique Objects. Instead, the Object.hashCode method is free to return non-unique hashCodes for unique Objects.
If you take a look at the source code for the Object.hashCode method you will see the following.
public native int hashCode();
The Object.hashCode method is native so the implementation is platform dependent.
anushree ari
Ranch Hand

Joined: Sep 30, 2002
Posts: 98
dan i got the concepts but the result for first and second both are "true", i do know how?

thx
dragon ji
Ranch Hand

Joined: Oct 31, 2002
Posts: 110
I think the option 1 is right just because that is the same as the api doc at SUN.
Follow is the quote:
As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)
please correct me!


scjp 1.4<br />challenge haven't limit!
anushree ari
Ranch Hand

Joined: Sep 30, 2002
Posts: 98
ji,
According to sun quite, the hashCode method defined by class Object does return distinct integers for distinct objects,
if a.equals{b)(if a and b are different objects) return true and b.equals(a)return true then a.hashcode(}==b.hashcode must true, but opposite is not true right.correct me
thx
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Originally posted by anushree ari:

According to sun quite, the hashCode method defined by class Object does return distinct integers for distinct objects,

The correct response to the question, "Are unique objects required to have unique hashcodes", is false.
If you ask the question, "Does the Object.hashCode method usually return unique hashcodes for unique objects", the answer is "usually true".
The correct response to the question, "Is the Object.hashCode method guaranteed to return unique hashcodes for unique objects", the answer is false. There is no guarantee. However, we know that it usually does.
anushree ari
Ranch Hand

Joined: Sep 30, 2002
Posts: 98
dan what about the second code
"For two object references referring to the same object, the hashCode method returns a same Integer".
this is for == operator, how this overrides the hashcode method.
david eberhardt
Ranch Hand

Joined: Jul 02, 2002
Posts: 158
Originally posted by anushree ari:
dan what about the second code
"For two object references referring to the same object, the hashCode method returns a same Integer".
this is for == operator, how this overrides the hashcode method.

anushree,
if the 2 references are pointing at the same 1 object, then the hash code gets computed on the same object and should return the same hashcode -
let's set up a scenario:

output for the above code is:
C:\BIN>javac Doggy.java
C:\BIN>java Doggy
david.equals(anushree) = false
anushree.equals(david) = false
david.hashCode() = 6298545
anushree.hashCode() = 28980466
now point anushree at the david object with 'anushree = david;'
david.equals(anushree) = true
anushree.equals(david) = true
david.hashCode() = 6298545
anushree.hashCode() = 6298545
C:\BIN>
david eberhardt
Ranch Hand

Joined: Jul 02, 2002
Posts: 158
note about the Object class's equals():

public boolean equals(Object obj) {
// equals method inherited from Objectclass...for any reference values x and y, this method returns true if and only if x and y refer to the same object
(x==y has the value true).
} */

this is what is known as a "shallow comparison" and is why the equals () is usually overriden in other classes.
IF I had overriden the equals () and hashCode () methods in my example above - the answer remains the same ...
IF 2 object references are pointing at the same object (refA == refB), then the hashCode() method should return the same value.
(assumes we honor the contracts when overriding the equals and hashCode methods in that class)
anushree ari
Ranch Hand

Joined: Sep 30, 2002
Posts: 98
thx david
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hashCode
 
Similar Threads
hashcode
TreeSet and compareTo Java 5
hashCode Question
Hash code
HashCode same for strings with same values - Why?