aspose file tools*
The moose likes Java in General and the fly likes Confusion on equals() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Confusion on equals() method" Watch "Confusion on equals() method" New topic
Author

Confusion on equals() method

dileep keely
Ranch Hand

Joined: Jun 28, 2010
Posts: 91
Hi All,
equals() method:It gnerally checks the conetnts are same or not.I am missing some basic scenario.Please comment on the above.
Thanks in advance.
class Test
{
String name;
Test(String name)
{
this.name=name;
}
public static void main(String[]args)
{
Test t=new Test("javaranch");
Test t1=new Test("javaranch");
System.out.println(t.equals(t1)+".-------->Test1");
Integer i=new Integer(10);
Integer i=new Integer(10);
System.out.println(i.equals(i1)+".-------->Integer");
}
}
output:
false.-------->Test1
true.-------->Integer
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
I take it you were expecting a different result? Which one, and why?

Class Integer overrides equals, whereas class Test does not - so it inherits an equals method from the Object class. Check the javadocs of both classes for a description of what each method does.
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1776
    
    7

The Test class doesn't override the equals() implementation of java.lang.Object. The Integer class does. When you invoke equals() on an instance of Test the implementation will check if the reference you invoke the method on and the reference you supplied to the equals() method refer to the same object. They do not, so the method returns false. In case of the Integer instances the values are compared rather than the references, because that's what the overridden implementation does. So, if you want instances of Test to perform a similar comparison, you should override the equals() method and return a result based on a comparison of the "name" member.

Edit: Ugh, too late.

Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Avishkar Nikale
Ranch Hand

Joined: Aug 06, 2010
Posts: 173
Dileep,

You will have to override the method & check the contents yourself.

The language will not do it for you.


Regards,
Avishkar Nikale
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
Avishkar Nikale wrote:Dileep,

You will have to override the method & check the contents yourself.

The language will not do it for you.
You should also override hashCode.
dileep keely
Ranch Hand

Joined: Jun 28, 2010
Posts: 91


output:
true 10 17523401
false 10 8567361

Jelle klap:
Thanks for your comment!!
I have done according to the suggestion(overriding equals() and hashcode()).I have a query here.
you have commented that
"When we invoke equals() on an instance of Test the implementation will check if the reference you invoke the method on and the reference you supplied to the equals() method refer to the same object".
It implies that it should also satisfy == comparison.If so in the above output I got (false for == and true for equals() comparison
which shouldn't be the case then? because == checks if two references are to the same object)

Anyone can help me out in this?Apologies if my understanding is wrong.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
dileep keely wrote: . . . Anyone can help me out in this? . . .
You are more likely to get help on code with the code tags on, so we can actually read it.

And I don't think your equals() method is anything like correctly implemented.
dileep keely
Ranch Hand

Joined: Jun 28, 2010
Posts: 91
I have inserted the code tags can you please suggest
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4422
    
    8

The main problem seems to be this:
That means that you consider any two ShowHash objects to be equal, regardless of what data they contain. Is that what you intended?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
Matthew Brown wrote: . . . you consider any two ShowHash objects to be equal, regardless of what data they contain. Is that what you intended?
Of course he didn't. That was th error I noticed earlier.
dileep keely
Ranch Hand

Joined: Jun 28, 2010
Posts: 91
equals() method is now correctly implemented.
Now please clarify my doubt on equals and == (as described above as per my understanding equals compare the contents and not the reference ).
Thank you for your time
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
dileep keely wrote:

output:
true 10 17523401
false 10 8567361

Jelle klap:
Thanks for your comment!!
I have done according to the suggestion(overriding equals() and hashcode()).I have a query here.
you have commented that
"When we invoke equals() on an instance of Test the implementation will check if the reference you invoke the method on and the reference you supplied to the equals() method refer to the same object".
It implies that it should also satisfy == comparison.If so in the above output I got (false for == and true for equals() comparison
which shouldn't be the case then? because == checks if two references are to the same object)

Anyone can help me out in this?Apologies if my understanding is wrong.
Please don't try altering old posts. It would have made my post and Matthew Brown's look like nonsense. Please post any changes in a new posting.

I have edited your post to return it to its original state.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
dileep keely wrote:equals() method is now correctly implemented. . . .
Since I have removed your emendation, please post the correct version again.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
And you appear to be correct that an equals() method tests whether two objects are identical, whereas == on objects tests whether they are both the same object.

Now find out why using the instanceof operator is unreliable.
dileep keely
Ranch Hand

Joined: Jun 28, 2010
Posts: 91
Because,
getClass() only gives equality if the two classes are identical-->when we are sure that we are comparing two objects.
whereas instanceof works for subtype(inheritance)-->when comparing two objects of different classes.
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

Dileep, Can we see the new implementation of the equals() method?


OCPJP 6
dileep keely
Ranch Hand

Joined: Jun 28, 2010
Posts: 91
Campbell,I will take care going forward.
Here is my new implementation of equals() method.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
That will of course throw a ClassCastException if you pass an Object of any other type. Remember the equals() method must accept arguments of any reference type, or null.
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

Yeah....may be you want to do this..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
Vinoth Kumar Kannan wrote:Yeah....may be you want to do this..
Actually you want to change the keyword if to the keywordreturn and you have the entire method in one line.

Now work out why. Also work out why the instanceof operator is not reliable in this instance.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confusion on equals() method