• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Incorrect equals() implementation...

 
RAGU KANNAN
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

return str.equals(obj);


And also the str may be null.
 
Pad Ven
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[/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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic