• 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
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

The way to compare HashCodes == or equals?

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I compare the hashcodes of two objects, should I really use the == comparetor? Since I think that checks if the two references point to the same object. Should not it be something like

x.hashCode().equals(y.hashCode()) // right or wrong ???

I know in the texts always '==' is used, but it seems a bit strange since we have to see if they are equivalent, and not if they are the same, right? Or am I lost now?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A hashCode is just a primitive int, so a simple value comparison (==) is appropriate.

In fact, obj.hashCode().equals... won't even work, because you can't dereference an int. (That is, you can't treat an int value as an object reference -- like trying to call a method on it.)
 
Marc Wentink
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, it's just an int!
Thanks, that makes it clearer!
 
Marc Wentink
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> In fact, obj.hashCode().equals... won't even work, because you can't
> dereference an int.

It's not really important I think, but may-be it will get autoboxed from int to Integer? And then the equals method would work.
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Marc Wentink:
but may-be it will get autoboxed from int to Integer?

Try it.
 
Marc Wentink
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok manfred here it is. I totally understand it now, I think.

class Test {
public int i;
public int hashCode() {return 1;}
public boolean equals(Object o)
{Test t = (Test) o; return t.i == i;}
Test(int p_i) {i=p_i;}
public static void main(String args[]) {
Test t1= new Test(1);
Test t2= new Test(1);
//t1.hashCode().equals(t2.hashCode()); //does not work: int cannot be dereferenced
Integer IT3 = t1.hashCode();
if (IT3.equals(t2.hashCode()))
System.out.print("Yes"); // output here is Yes though
else
System.out.print("No");

}
}
 
reply
    Bookmark Topic Watch Topic
  • New Topic