aspose file tools*
The moose likes Beginning Java and the fly likes Overiding Equals Method. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Overiding Equals Method." Watch "Overiding Equals Method." New topic
Author

Overiding Equals Method.

Suresh Rajadurai
Ranch Hand

Joined: Feb 22, 2007
Posts: 58
Hi Guys,

I have a doubt. When I compile and run this program, it gives the o/p

True
9
8.

Actually this program is creating 2 objects of same type with 2 different parameters (8 and 9) and comparing the 2 objects with overridden Equals method. Could anyone please explain me, why "this.moofValue" is getting the value "8" when it actually passes "9". (line marked XXX)


Thanks & Regards


Suresh.

public class EqualsTest
{
public static void main (String [] args)
{
Moof one = new Moof(8);
Moof two = new Moof(9);
if (one.equals(two))
{
System.out.println("one and two are equal");
}
}
}


class Moof
{
private int moofValue;
Moof(int val)
{
moofValue = val;
}

public int getMoofValue()
{
return moofValue;
}

public boolean equals(Object o)
{
System.out.println(o instanceof Moof);
System.out.println(((Moof)o).getMoofValue());
System.out.println(this.moofValue); //XXX
if ((o instanceof Moof) && (((Moof)o).getMoofValue() == this.moofValue))
{
return true;
}
else
{
return false;
}
}
}
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433



Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
vijay shanker
Ranch Hand

Joined: Oct 26, 2007
Posts: 88
hi suresh,


look at the code what you are doing is below


here one.equals(two) method is called. as object two is passed in parameter with that object moofValue which is passed is 9.
when you call

what is called is getMoofValue() on object two actually.

and then what you do is



moofValue of object one.
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

Please check the following image, I hope this will clear your doubt :

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
I am afraid I can't see the image properly; it is to small and the link doesn't seem to work. Never mind, the implemntation of equals() isn't very good.

Look at this Chapter from Bloch Effective Java, and this recent website, which I found from Google. Both these tell you the risky nature of using instanceof in an equals() method, and how it might violate the contract of equals(). Note there is a lot of discussion following the blogspot quote.

The way I would do it, which ensures strict reflexivity and strict class-sensitivity, is something like thisThere is no need for get methods to access the fields. You have to verify that there is no risk of any instance fields which are reference types being null at this stage, and also that these fields have their own equals() [and hashCode()] method.
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

at my side, Image is working perfectly fine,anyways please find the below URL, may be it works at your side:

http://img214.imageshack.us/my.php?image=ranchax0.jpg

http://img214.imageshack.us/my.php?image=ranchax0.jpg
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
Originally posted by Sunny Jain:
at my side, Image is working perfectly . . .
Yes, that is working nicely. Thank you.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

That's great guys.

Thanks for the links CR.

Fyi, the other two good links which are there in the blog entry are here:

  • Secrets of equals() - Part 1 - http://www.angelikalanger.com/Articles/JavaSolutions/SecretsOfEquals/Equals.html
  • Secrets of equals() - Part 2 - http://www.angelikalanger.com/Articles/JavaSolutions/SecretsOfEquals/Equals-2.html
  • instanceof versus getClass in equals Methods - http://www.artima.com/intv/bloch17.html


  • Hope it may add a little value here


    Everything has got its own deadline including one's EGO!
    [CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
    Raghavan Muthu
    Ranch Hand

    Joined: Apr 20, 2006
    Posts: 3344

    Suresh Rajadurai ,

    Kindly make use of the Code Tags when pasting java code so that it will look neatly!
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Overiding Equals Method.
     
    Similar Threads
    hashCode values
    instanceof
    Not able to understand the program
    Want hepl to understand Implementin an equals()
    instance of problem