jQuery in Action, 2nd edition*
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 Murach's Java Servlets and JSP this week in the Servlets 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: 4163
    
  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: 4163
    
  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: 37877
    
  22
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.
 
Don't get me started about those stupid light bulbs.
 
subject: what is difference between equals() and contentEquals() of String class?
 
Similar Threads
How to Compare StringBuffer object with String Object
difference between equals() and == and compareTo()
difference between equals, contentequals in Strings?
contentEquals in java.lang.String
what is the difference between == and .equals() method????