Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

equals( )

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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");
}
}
}
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 179
Mac Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79637
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's all in the API documentation.
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that the equals method is overridden in the String class but inherited in the StringBuffer class.
 
Campbell Ritchie
Marshal
Posts: 79637
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I Thank You All for yor precious help !!!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic