File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confusion on equals() method

 
dileep keely
Ranch Hand
Posts: 108
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1337
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1951
7
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Avishkar Nikale
Ranch Hand
Posts: 173
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dileep,

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

The language will not do it for you.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 108
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 108
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have inserted the code tags can you please suggest
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 108
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dileep keely wrote:equals() method is now correctly implemented. . . .
Since I have removed your emendation, please post the correct version again.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 108
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 276
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dileep, Can we see the new implementation of the equals() method?
 
dileep keely
Ranch Hand
Posts: 108
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell,I will take care going forward.
Here is my new implementation of equals() method.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 276
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah....may be you want to do this..
 
Campbell Ritchie
Sheriff
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic