Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Use toString() to test if StringBuilder objects are equal

 
Ranch Hand
Posts: 104
  • 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.

 
Ranch Hand
Posts: 49
1
Tomcat Server Chrome Java
  • 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
Tomcat Server Chrome Java
  • 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.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • 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.
 
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: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • 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: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • 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
Boost this thread!