Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

difference between equals, contentequals in Strings?

 
siva chaitanya
Ranch Hand
Posts: 59
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both are used to check whether two strings are equal or not...Please explain i cant able to figure it out..
 
Jelle Klap
Bartender
Posts: 1951
7
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The contentEquals() methods are convenience methods for comparing the character sequence that the String represents to the character sequence that a StringBuffer or CharSequence implementation represents.
The equals() implementation of String does conceptually the same thing, but it can only do so for two String instances. Passing anything other than an instance of String to the equals() method would yield false.
The rules established in the general equals() contract would make it impossible to implement equals() any other way, and furthermore the equals() implementation of String predates the existence of the CharSequence interface and some of its implementations.
 
Winston Gutkowski
Bartender
Pie
Posts: 9490
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
siva chaitanya wrote:Both are used to check whether two strings are equal or not...Please explain i cant able to figure it out..

equals() can only compare two Strings; contentEquals() takes a CharSequence, and so can be used to compare a String with any CharSequence object (which also includes another String). From what I can see, if contentEquals() IS called with another String, it will produce exactly the same result as equals() (although it probably won't be quite as fast).

Winston

[Edit] Dang! Too slow.
 
Rajdeep Biswas
Ranch Hand
Posts: 187
Eclipse IDE Java Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you see the prototypes:

public boolean equals(Object obj), and public boolean contentEquals(CharSequence cs).

Think simply, you can use equals method to compare any Object, but contentEquals method compares only strings here...
In java.lang.String class, the toString() method is overridden, so it returns the value in the object, i.e., some String, and not String@12avbe (some hash code), so using both these methods for String would yield same results in my sense, but while you are comparing for other classes which do not implement toString() method in a recommended way (return the contents of the object), differences may be pronounced loud....

You have to read the API for understanding: java.lang.String

Regards
 
Winston Gutkowski
Bartender
Pie
Posts: 9490
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajdeep Biswas wrote:If you see the prototypes:
public boolean equals(Object obj), and public boolean contentEquals(CharSequence cs).
Think simply, you can use equals method to compare any Object, but contentEquals method compares only strings here...

You're right in principle, but the fact is that a properly written equals() method generally only allows comparisons between like objects (ie, objects in the same hierarchy); and since String is final, the only "like" type is another String.

As for all that stuff about toString(): it has no bearing on the case whatsoever. What Siva needs to do is read the documentation for String.equals() and String.contentEquals() because it explains exactly what the comparisons do.

Winston
 
Lisa Dissousa
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you guys explain me with a example i gone through the documentation but it provides only definition
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you really not work it out?
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic