wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes A queastion about equals () & hashCode() methods 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 "A queastion about equals () & hashCode() methods" Watch "A queastion about equals () & hashCode() methods" New topic
Author

A queastion about equals () & hashCode() methods

Nasser Fayeghi Fard
Greenhorn

Joined: Sep 16, 2009
Posts: 20
Hi All,

I've written some code to investigate euals () & hashCode() methods. Here, I want to check if we have a hashCode method that always return a constant value, is equals() method return true?



I expected 'true', but it prints false. Does anybody have any idea about internal of equals () mehotd? In which condition I can manipulate (Object) methods that it returns true for two different objects?


Thanks for any idea
Nasseria


SCJP 6.0 (80%), SCWCD (88%)
Now preparing for SCEA-1
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

I expected 'true'

Why did you expect true, the equals method in the Object class doesn't use the hashCode method to find if objects are equal or not. It uses a simple == comparison on objects, so it will return true only if you call it with the same object that you pass it


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Garlapati Ravi
Ranch Hand

Joined: Mar 05, 2008
Posts: 171
in Heap they are created as two different objects that's why it's false, as t1 and t2 are poinitng to two two different objects.
if you use t1=t2; in your code then it results true, as t1 and t2 points to same object on Heap.
Aj Deschanel
Ranch Hand

Joined: Oct 20, 2009
Posts: 40
The Specs say: If you do not override the "equals()" method, then your class will use "==" to check the objects equality. So for your particular case it's normal to get False.
It's very easy to understand if you take a look at the code of "equals()" method from "Object.java"


SCJP 1.6, SCWCD 1.5
Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 255

Hey Nasser,

The simplest explanation of this behavior would be to identify the definition of equals function. As you have not overridden the objects equal method, t1.equals(t2) would use the equals function of Object class.

The equals method of Object class checks the equality using == operator and returns whether both the references are pointing to the same object or not.

Due to this you are getting false as a result. I hope, this would help you to get your doubts clear

Regards
Salil Verma
Nasser Fayeghi Fard
Greenhorn

Joined: Sep 16, 2009
Posts: 20
Aj Deschanel wrote:The Specs say: If you do not override the "equals()" method, then your class will use "==" to check the objects equality. So for your particular case it's normal to get False.
It's very easy to understand if you take a look at the code of "equals()" method from "Object.java"


Thanks Aj Deschanel & Salil Vverma
It was exactly what I was looking for default behaviour of of equals () that as you mentioned is based on comparison between two references using ==

Thanks All guys for sharing
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A queastion about equals () & hashCode() methods