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 Incorrect equals() implementation... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Incorrect equals() implementation..." Watch "Incorrect equals() implementation..." New topic
Author

Incorrect equals() implementation...

RAGU KANNAN
Ranch Hand

Joined: Dec 16, 2005
Posts: 103
Hello Guru�s,

Why this equals method implemented incorrectly? The following code compares string literals against obj values. So what is the problem? Pls explain to me.

Thanks, Raghu.K

public class BetterString
{
String str;
public BetterString(String s)
{
str = s;
}
public boolean equals(Object obj)
{
if(obj instanceof BetterString)
{
if(obj == this)
return true;
else
return str.equals(obj);
}
else if(obj instanceof String)
{
return str.equals(obj);
}
return false;
}
}
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Look at this line.



If you send a BetterString object to the equals method, then this line is going to return false no matter what.

Remmber that if you call the equals method on a String object and send a reference to an object that isn't a String to it, the result is false.
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023

return str.equals(obj);


And also the str may be null.
Pad Ven
Ranch Hand

Joined: Apr 29, 2005
Posts: 38
But I got true for the following code. That equals() method does not return false always.

public class BetterString
{
String str;
public BetterString(String s)
{
str = s;
}
public boolean equals(Object obj)
{
if(obj instanceof BetterString)
{
if(obj == this)
return true;
else
return str.equals(obj);
}
else if(obj instanceof String)
{
return str.equals(obj);
}
return false;
}

public static void main (String[] args) {
BetterString bs = new BetterString ("OK");
System.out.println (bs.equals (new String ("OK")));
}
}
Suhas Wadadekar
Ranch Hand

Joined: May 16, 2006
Posts: 95

System.out.println (bs.equals (new String ("OK")));
}
}


1. If you pass a String object then str.equals(obj) will be true if and only if in the above quote you write "OK".
2. Also u can pass only a String object to make the line str.equals(obj) work, passing a BetterString will always return false
Vipin Das
Ranch Hand

Joined: Jul 05, 2004
Posts: 47
If you send a BetterString object to the equals method, then this line is going to return false no matter what.



Just to argue it wont be false all time, the following code will also return true. Append the following line to the padmanabhan's code.
Vijay Raj
Ranch Hand

Joined: Oct 10, 2005
Posts: 110
He's calling the equals() method on the String object. So passing anything other than a String reference, it will return false.

What you are doing is passing the BetterString object to BetterString's over-ridden equals() method. What Keith Lynn is saying that passing BetterString object to String's equals() method will always return false.

regards,
vijay.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
[/qb]<hr></blockquote>

That is a different situation.

The reason that works is because of this statement.



The condition in the else if will only be checked if you send a String object to the equals method.

But that is not the purpose of the equals method of BetterString.

The intention is to determine when one BetterString object is equal to another.

bs.equals(bs) only returns true because of this.



which does not test the contents of the BetterString object.
[ July 12, 2006: Message edited by: Keith Lynn ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Incorrect equals() implementation...