wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes difference between equals, contentequals  in Strings? 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 » Beginning Java
Bookmark "difference between equals, contentequals  in Strings?" Watch "difference between equals, contentequals  in Strings?" New topic
Author

difference between equals, contentequals in Strings?

siva chaitanya
Ranch Hand

Joined: Jul 05, 2011
Posts: 59
Both are used to check whether two strings are equal or not...Please explain i cant able to figure it out..
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1752
    
    7

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.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7484
    
  18

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.


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Rajdeep Biswas
Ranch Hand

Joined: Mar 26, 2012
Posts: 186

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


The biggest gamble will be to ask a question whose answer you know in that it will challenge your theory | www.TechAspire.blogspot.in
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7484
    
  18

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

Joined: Sep 18, 2012
Posts: 3
can you guys explain me with a example i gone through the documentation but it provides only definition
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37874
    
  22
Can you really not work it out?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: difference between equals, contentequals in Strings?
 
Similar Threads
Hashmap
a question
select into outfile
Using commandline arguments Query
java.lang.ArrayStoreException (S.O.S)