File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes difference between equal and compare methods of String class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "difference between equal and compare methods of String class" Watch "difference between equal and compare methods of String class" New topic
Author

difference between equal and compare methods of String class

vijay shanker
Ranch Hand

Joined: Oct 26, 2007
Posts: 88
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

Joined: Feb 19, 2008
Posts: 2902
    
    1

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 !


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

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.....


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3162
    
  10
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.


Joanne
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

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

Joined: Oct 13, 2005
Posts: 36453
    
  15
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

    Joined: Oct 27, 2005
    Posts: 19541
        
      16

    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.


    SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
    How To Ask Questions How To Answer Questions
    sachin
    Greenhorn

    Joined: Dec 04, 2008
    Posts: 1
    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

    Joined: Oct 13, 2005
    Posts: 36453
        
      15
    "java sac" please read the important administrative private message I have just sent you.

    CR
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 36453
        
      15
    "sachin" I am afraid that is no better.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 36453
        
      15
    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.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: difference between equal and compare methods of String class
     
    Similar Threads
    Doubt with String Methods
    difference between the methods on session object
    what's the difference between these two methods?
    JATO Vs JDOM
    difference between encodeRedirectURL and encodeURL