• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

hashCode

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ari
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
iam sorry it's in the topic of hashcode,
thx
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
anushree ari
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
dan i got the concepts but the result for first and second both are "true", i do know how?

thx
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
anushree ari
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thx david
 
reply
    Bookmark Topic Watch Topic
  • New Topic