Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why equals() not overridden in StringBuffer

 
Singhal Anuj
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why java.lang.String overrides equals() method, but java.lang.StringBuffer does not?
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String is considered one of the 'Value' classes (not sure if that's my term or if I picked it up somewhere). This includes all the primitive wrapper classes and String. They share some characteristics.

They are all immutible. They all override equals, hashCode, and toString. I think they also have some other common methods.

To my knowledge no other class in the standard API overrides equals. I could be wrong here, but I think that's right.
 
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
No, there are plenty of mutable classes that override equals(); all the java.util Collections classes do, for example.

My guess: they forgot, and then left it that way so as not to break existing code. I think having SB override equals() would be perfectly OK.
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I stand corrected, thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic