• Post Reply Bookmark Topic Watch Topic
  • New Topic

.equals override  RSS feed

 
satish bejju
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in all wrapper classes and string the .equals method is overridden then why not in strinbuffer
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because wrappers and strings are immutable. If you have two objects that are the same, you know they will always be the same.

Whereas with StringBuffer I could append to one of the objects right after you check for equality.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Because wrappers and strings are immutable. If you have two objects that are the same, you know they will always be the same.

Whereas with StringBuffer I could append to one of the objects right after you check for equality.


I don't think that's the reason. There are plenty of mutable classes with reasonable equals() semantics--Date, Collections and Maps to name a few.

As with most "Why is Java this way?" questions, ultimately the answer is, "Because that's the decision the designer's made." Unless we can talk to them or get hold of a white paper or the minutes of their meetings, we can only speculate as to why that decision was made.

In the case of StringBuffer and StringBuilder, one reasonable explanation seems to be that they tend to be fairly short-lived. Their jobs are just to put the pieces together to create Strings. Ultimately, it's the Strings' contents we care about, not usually the intermediate stages as they're being assembled. Combine that with the fact that, if you want to know if two SBs have the same characters in them--that is, compare them like Strings--it's trivial to do sb1.toString().equals(sb2.toString()). Again, though, I'm just guessing.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good point. I actually don't think immutability is the reason either. I was trying to think about why it was different than String. (sorry that wasn't clear.)
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read somewhere that it is because StringBuilder is intended to change rapidly and frequently. But I am would have preferred it to have an overridden equals() methods because it may be necessary to check whether their content is the same. Agree with Jeff, that the toString method allows such equality to be tested very easily, however.
 
satish bejju
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for replies but still not clear

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
satish bejju wrote:thanks for replies but still not clear


What's not clear? It should be clear by now that it's unlikely that anybody on this forum will know the reason for sure. We've given you some possibilities. What does it matter if you learn the real reason?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!