• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

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

 
Greenhorn
Posts: 20
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
Greenhorn
Posts: 20
jQuery Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4179
22
IntelliJ IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 80780
489
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Soumya Padhiary
Greenhorn
Posts: 20
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you a lot.
 
Soumya Padhiary
Greenhorn
Posts: 20
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic