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 doubt in 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 "doubt in hashcode..." Watch "doubt in hashcode..." New topic
Author

doubt in hashcode...

Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
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 ]

SCJP5 and SCWCD1.5
Think Twice Act Wise...
M Srilatha
Ranch Hand

Joined: Aug 27, 2008
Posts: 137
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!


Thanks,<br />Srilatha M
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
you mean there is no contract in between == and hashcode..

only exists in between equals and hashcode...


am i right?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

yes ganesh, == is used to check for reference equality. It has nothing to do with hashCode or equals...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
ok i got it....

thanku sri and ankit....
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8815
    
    5
Hi Ganeshkumar,

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

Thanks,

Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
yeah i have mentioned it now....
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: doubt in hashcode...