• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

equals() in StringBuffer

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai Everyone,
Please have a look at the following code.

class Test{
public static void main(String args[]){
StringBuffer s=new StringBuffer("java");
StringBuffer s1=new StringBuffer("java");
System.out.println(s.equals(s1));
}
}

equals() of the above StringBuffers returns false.But both of them has the same value "java". So equals() should return true. Can anyone explaim me why the output is false.

Thanks
sudha
 
Author
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!
Have a look to the APIs of String and StringBuffer.
The class StringBuffer does not override the equals method of Object, so instances of StringBuffer have the same behaviour of Object, when they are compared via equals (in Object, this method returns true if and only if the references refer to the same object).
String overrides the method equals, it compares the content of the Strings.
Greetings from Hamburg, Stefan
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In addition to Stephan's reply, you can use the following to get what you want:
 
There is no "i" in denial. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic