• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

not clear with hashcodes

 
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I read about hashcode from many books & refernce but atill have confusion.

this i1 & i are 2 different objects but equals() still returns true .
Same thing if do using 2 Objects of same state var ,it shows unequality.

Integer i=new Integer(1);
Integer i1=new Integer(1);
System.out.println("equality "+i.equals(i1));
System.out.println("hashcode "+i.hashCode()+i1.hashCode());
Please explain me how its showing this output.

[ June 26, 2007: Message edited by: Lucky J Verma ]
[ June 26, 2007: Message edited by: Lucky J Verma ]
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I run the code, the hashCodes are the same (1). What version of Java are you using.

Any objects that are .equal() should have the same hashCode; you will run into odd problems using collections if they aren't
 
Lucky J Verma
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh ,i am really sorry i did a typo ..hashcodes are same .But my equals() withInteger sould not be true.i am not really clear. or why it should be true.I am not very clear about it .As for other objects it gives false ,even if state is same.
i will correct my posts .

Thnx
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java.lang.Integer overrides equals() and hashCode() so that all Integer objects with the same int value are equal, and all have the same hashCode(). That's exactly what you're supposed to do when you override equals() and hashCode().

If equals() never returned true for two different objects, then it wouldn't be necessary -- you could just always use ==. Right?
 
Lucky J Verma
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes,now i got it .
Thanks a lot :-)
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic