File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

difference between equal and compare methods of String class

 
vijay shanker
Ranch Hand
Posts: 88
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi friends,

i just got to be in thinking, if there is any difference between those to above written methods. as i am not able to understand due to lack of concentration today.

can any one please simplify the difference between both.

cheers

vijay shanker
 
Sagar Rohankar
Ranch Hand
Posts: 2904
1
Java Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
contains() checks for a sub string , where as equals() checks for whole string , and other difference is , contains() takes object of CharSequence class where as equals() takes Object as its parameter !
 
Ankit Garg
Sheriff
Posts: 9496
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sagar Rohankar:
contains() checks for a sub string , where as equals() checks for whole string , and other difference is , contains() takes object of CharSequence class where as equals() takes Object as its parameter !


He was actually trying to ask for compareTo and equals(I think)....

Well the equals method finds that two string are equal or not. If they are equal, then true is returned otherwise false is returned.

In case of compareTo method it checks whether two strings are equal. If they are equal, then 0 is returned. If the strings are not equal then the result is a negative integer if this String object lexicographically precedes the argument string. The result is a positive integer if this String object lexicographically follows the argument string(taken from the documentation)

This means that if you do this "a".compareTo("b"); then the result will be -1. if you do this "a".compareTo("c"); then the result will be -2. if you do this "a".compareTo("z"); then the result will be -25. if you do this "z".compareTo("y"); then the result will be 1. if you do this "z".compareTo("a"); then the result will be 25.

I think you will get it.....
 
Joanne Neal
Rancher
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compareTo method is there because String implements the Comparable interface. It is intended to indicate whether one String is less than, equal to or greater than another String. It is mainly used by sorting algorithms to determine the order of two objects.
 
Sagar Rohankar
Ranch Hand
Posts: 2904
1
Java Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ohh, sorry I think I need a new sets of spectacles, What Ankit says is fine,

String#equals() - checks two strings equality from start to end .

String#compareTo() - checks the two strings character sequence (lexicography ) , one by one, where mismatch happened its return the difference int value .
 
Campbell Ritchie
Sheriff
Pie
Posts: 47231
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg is spot on. You will find all sorts of useful information about that if you
  • Read about the Object class
  • Read about the Comparable<T> interface
  • Search the Sun website; there is a sample chapter from Joshua Bloch's Effective Java there somewhere which tells more about equals, but probably not compareTo
  •  
    Rob Spoor
    Sheriff
    Pie
    Posts: 20372
    44
    Chrome Eclipse IDE Java Windows
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    vijay,

    you are correct in thinking that these two methods are quite related. Although it hasn't been done, equals could have been implemented using compareTo:

    Also, s1.equals(s2) == true implies s1.compareTo(s2) == 0 (and vice versa), and s1.equals(s2) == false implies s1.compareTo(s2) != 0 (and vice versa).

    However, and this is important, this does not have to be the case for all classes. It is for String, but no rule prohibits different natural orders for other classes.
     
    sachin
    Greenhorn
    Posts: 1
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    string1.equals(string2) and string1.compareTo(string2)==0 are same.

    But difference in behaviour is when string2 is null
    then 'equals' returns false but 'compareTo' throws an exception.
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 47231
    52
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    "java sac" please read the important administrative private message I have just sent you.

    CR
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 47231
    52
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    "sachin" I am afraid that is no better.
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 47231
    52
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    We do take the naming policy seriously; one person has had their account closed today for not complying; please alter your displayed name in accordance with the naming policy.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic