• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Use toString() to test if StringBuilder objects are equal

 
Thomas Hauck
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question: Is the correct way to determine if 2 StringBuilder objects are equal to use
1) the toString() method and then
2) equals()
as is done on //line 4 ?





When you use the equals() method on 2 StringBuilder objects the code compiles.

I read that StringBuilder does not override Object's .equals() function, which means the two object references are not the same and the result is false
as shown in the output of line 1.

 
Krishna Kanth
Ranch Hand
Posts: 49
1
Chrome Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
StringBuilder indeed does not implement its own equals() method and inherits from Object class.

Converting to String and using equals method of String will work. you can probably add a few more conditions like (not adding null checks yet :-))
sb1 == sb2 || (sb1.length() == sb2.length() && (sb1.toString().equals(sb2.toString)))
 
Krishna Kanth
Ranch Hand
Posts: 49
1
Chrome Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alternatively you can read contents of StringBuilder objects and compare them too, if you are an optimization guy.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to know if 2 StringBuilder objects are equal, just use the == operator or the equals() method. If you want to know if 2 StringBuilder objects contain the same sequence of characters, using s1.toString().equals(s2.toString()) is the most straightforward way to give you the correct answer.
 
Aki Mohan
Ranch Hand
Posts: 99
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any difference when comparing String or StringBuilder objects? Like I will use(for String) == to see if two references point to the same object or equals method to see if the Objects contain the same contents. We can do the same for StringBuilder, right?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aki Mohan wrote:Is there any difference when comparing String or StringBuilder objects? Like I will use(for String) == to see if two references point to the same object or equals method to see if the Objects contain the same contents. We can do the same for StringBuilder, right?

1. Use == operator to determine if 2 references point to the same object
2. Use equals method to determine if 2 objects are meaningfully equal (have the same contents). This will only work if the class defines its own equals method. If the class doesn't define an equals method, the equals method from the Object class is used (and it uses == operator). So the equals method of the Object class only returns true if both references point to same object.

The String class defines its own equals method, the StringBuilder class does not. That's shown in the following example:
 
Aki Mohan
Ranch Hand
Posts: 99
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, thanks Roel. StringBuilder inherits the Object's equlas method and thus in both the cases it uses == method. Got this, I appreciate your help.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aki Mohan wrote:Got this, I appreciate your help.

Glad I could help. Don't forget to like (+1) the answer
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic