File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes what is difference between equals() and contentEquals() of String class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "what is difference between equals() and contentEquals() of String class?" Watch "what is difference between equals() and contentEquals() of String class?" New topic
Author

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

Soumya Padhiary
Greenhorn

Joined: Jan 10, 2013
Posts: 20

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

Joined: May 12, 2012
Posts: 20

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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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

Joined: Oct 13, 2005
Posts: 39436
    
  28
And welcome to the Ranch
Soumya Padhiary
Greenhorn

Joined: Jan 10, 2013
Posts: 20

thank you a lot.
Soumya Padhiary
Greenhorn

Joined: Jan 10, 2013
Posts: 20

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what is difference between equals() and contentEquals() of String class?