• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

equals( )

 
Rekha Anand
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

The following program prints "Both are not equal"... I cannot understand WHY? Please help.



public class T3
{
public static void main(String arg[])
{
String str = "Java";
StringBuffer buffer = new StringBuffer(str);
if(str.equals(buffer))
{
System.out.println("Both are equal");
}
else
{
System.out.println("Both are not equal");
}
}
}
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because String and StringBuffer are two completely different classes, and it's very bad practice for an implementation of equals() to return 'true' for instances of two different classes? I'm not sure why you think they should be the same.

Note that this evaluates to 'true':

if (str.equals(buffer.toString()))

i.e., if you convert the StringBuffer to a String, then the two are equal.
 
Ravikanth kolli
Ranch Hand
Posts: 179
Chrome Eclipse IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ernest,
I have a little question?

does buffer.toString() creates a new string object?
Or does it refer to the str in the string pool?
I was just wondering why does it return true when string str object is compared to buffer.toString()

Thanks
 
Campbell Ritchie
Sheriff
Pie
Posts: 49361
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's all in the API documentation.
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that the equals method is overridden in the String class but inherited in the StringBuffer class.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49361
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Roger Chung-Wee:
Note that the equals method is overridden in the String class but inherited in the StringBuffer class.
True. I have been caught out by that. If you look through the API documentation for StringBuilder you find "equals" in the block headed "inherited from Object" and in String you find the method in amongst all the other methods. That is how you can tell.

I enquired more about that, and found that Sun are of the opinion that something which changes all the time, like a StringBuilder (use StringBuilder instead of StringBuffer) ought not to have its own equals method. I am not convinced, but you can use
if(builder1.toString().equals(builder2.tostring()) . . .
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ravikanth kolli:
hi ernest,
I have a little question?

does buffer.toString() creates a new string object?
Or does it refer to the str in the string pool?
I was just wondering why does it return true when string str object is compared to buffer.toString()

Thanks


1) StringBuffer.toString() always returns a new String, each time you call it. It doesn't cache a reference to the original String you created it from, nor to any previous return value from toString().

2) The equals() method implemented by the String class returns true if the two Strings contain the same characters in the same order, even if they are two physically distinct String objects. If a class overrides equals(), that's generally why -- so that two "equivalent but distinct" objects can be considered equal.
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
equals() must be the same regardless of how it's called

a.equals(b) ---=same as=--- b.equals(a)

this is the generally reason an instance of one class should not say it's equal to an instance of a different class - the other class might not agree,


(as stated before) if you want to know if they're the same string value use toString() - and don't worry about the memory usage.
 
Rekha Anand
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I Thank You All for yor precious help !!!
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic