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

StringBuffer on overriding of equals

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello ,

I was just going through some previous topics and came across that StringBuffer does not override equals() . Any particualr reason as to why it does not ? Typically , it should override to check if the contents in it are the same just as String class does .

Thanks in advance
Sweta
 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because String is final class,but StringBuffer doesn't,Even as StringBuffer we can change the value
 
sweta doshi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you please elaborate on this ? I mean , how does that stop the StringBuffer from overriding equals ?

Thanks
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, i will try to answer this , I hope this is correct. But i will wait for what other ranchers have to say about this.
The equals() is overriden in String, Wrapper classes, because all these are immutable. So for Strings if a.equals(b) returns true that means contents of and b are same and also since a and b are strings its guaranteed that you cannot change the contents of strings, since strings are immutable.

String a = "String";
String b = "String";
a.equals(b) will return true for two reasons
a) Both a and b refer to same single "String" object in String pool.
b) Since strings are immutable its guarenteed that a.equals(b) will return true always, unless offcourse a is not reffereing to other string say "Text".

But for string buffer since StringBuffer objects are mutable that is the contents can be changed a.equals(b) will return false if a and b are referring to two different stringbuffer objects even though the contents are same, since its possible to change the contents of a with a call to append().

So i guess equals() is overridden to check the contents for immutable objects like String, Wrapper objects but not for mutable objects like StringBuffer.
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sweta doshi:
Can you please elaborate on this ? I mean , how does that stop the StringBuffer from overriding equals ?

Thanks



Admittedly, I don't understand the mutable argument either. While it is true that changing the equality and hash code will corrupt a hash map/set, if the mutable instance is used as a key, but so what?

IMHO, I think Sun should have overridden equals() and hashCode(), and let those, who choose to mutate the objects when they are used as keys, get burned.

Henry
 
CLUCK LIKE A CHICKEN! Now look at this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic