Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is difference between equals() and contentEquals() of String class?

 
Soumya Padhiary
Greenhorn
Posts: 20
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the difference between below 2 methods of String class:

boolean equals(Object o);
boolean contentEquals(CharacterSequence cs);

Please explain with an example .
 
Michael Lars
Greenhorn
Posts: 20
Java jQuery Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
equals(Object o) return true/false on any type of data, depend if the content is equal !

contentEquals(CharacterSequence cs) returns true if and only if this String represents the same sequence of characters as the specified StringBuffer.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The big difference is that equals() will only work with another String, while contentEquals() would work on any CharacterSequence (like StringBuilder).


Should produce:
str1.equals(str2): true
str1.contentEquals(str2): true
str1.equals(str3): false
str1.contentEquals(str3): true
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:The big difference is that equals() will only work with another String...

That is to say could only every produce TRUE with another String.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50258
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Soumya Padhiary
Greenhorn
Posts: 20
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you a lot.
 
Soumya Padhiary
Greenhorn
Posts: 20
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:
Steve Luke wrote:The big difference is that equals() will only work with another String...

That is to say could only every produce TRUE with another String.


thank you buddy.. its clearify my doubt.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic