Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Incorrect equals() implementation...

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
}
}
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


return str.equals(obj);



And also the str may be null.
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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")));
}
}
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic