• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt in hashcode...

 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SOURCE: http://www.coderanch.com/t/417745/java-programmer-SCJP/certification/hashCode-equals



here i am overridng hashcode method with return value 1.

so hashcode for objects are same and every object should go into same bucket....

but why n1==n2 is false here?

[ November 17, 2008: Message edited by: Ganeshkumar cheekati ]
 
M Srilatha
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but why n1==n2 is false here?


Here n1 and n2 are reference variables. And n1 and n2 are referring to two different objects on the heap. So n1==n2 will be false.
n1==n2 will always be false unless n1 and n2 referring to the same object on the heap.

when you say n1==n2, it wont compare the hashCode Values of the objects referred by n1 and n2 but it will check if both n1 and n2 are referring to the same object!

Hope this is clear!
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you mean there is no contract in between == and hashcode..

only exists in between equals and hashcode...


am i right?
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes ganesh, == is used to check for reference equality. It has nothing to do with hashCode or equals...
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok i got it....

thanku sri and ankit....
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ganeshkumar,

Please tell us the source of the questions you are discussing!!!

Thanks,

Bert
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah i have mentioned it now....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic