Strings in Java can be compared in following ways:-
1) Using == operator.
2) Using equals()
3) Using equalsIgnoreCase()
4) Using compareTo()
5) Using compareToIgnoreCase(String str)
1) By using == operator
When you compare two strings using ==, it only compares reference variables. It only checks whether the two reference variables are pointing to same String objects or not.
== DO NOT compare the CONTENTS of Strings. Often beginers do mistake of using == to compare strings for checking the equality of their contents. So never use == when you are comparing strings to match their contents as explained in the article String equality Check: equals() or == ?.
2) By using equals()
equals(), unlike ==, is used to compare the contents of two Strings. equals() compare two strings by doing character by character comparison and does case sensitive comparison.
3) By using equalsIgnoreCase()
equalsIgnoreCase() is same as equals() except that it ignores the case when comparing contents of strings.
So, "test".equalsIgnoreCase("TEST") will be true but for equals() it would be false:
"test".equals("TEST") // false.
equals() and equalsIgnoreCase() are described here.
4) By using compareTo()
compareTo() compares two strings lexicographically. This means compareTo() not only tells whether two strings have equal contents, but it also tells whether one string comes before or after the other string. This helps in situations where you need to sort the strings either in ascending or descending order.
Lexicographical order of strings is the same order in which words are arranged in a Dictionary.
String class overrides the compareTo() method from Comparable interface which it implements.
Actually while using compareTo() method the intention is not to check the equality of string data but to know that whether one string is less than or equal to or greater than other.
Suppose you are comparing two strings s and s1.Then,
i) If s.compareTo(s1) < 0
This implies that first string (s) should come before the argument string (s1).
Therefore s< s1.
ii) If s.compareTo(s1) == 0
Implies that first string (s) should is equal to argument string (s1) i.e same length and same contents.
iii) If s.compareTo(s1) > 0
This implies that first string (s) should come after the argument string (s1).
Therefore s1 < s.
According to JavaDocs while comparing strings lexicographically two cases can arise:
1) At least one character in two strings is different:
example,
"Rose".compareTo("Road");
In such case compareTo() will determine the position of first character which is different in two strings. Here it comes out at index 2 i.e character 's' and 'a'.
compareTo() then return the difference of these two characters from which you guess which string is smaller.
Here 's'-'a' comes out to be 18 which is the difference of their ascii values.
Since it is 18 > 0 first string is greater than argument string, therefore "Road" should come after "Rose" (Rose < Road).
2) Characters are same but one string is smaller in length than other:
Example,
"Bat".compareTo("Batman");
In this case there is no such first character which is different as "Bat" includes in "Batman". But "Bat" is smaller than "Batman" in length. Here compareTo() will return the difference of their lengths instead of the difference between their characters.
"Bat".compareTo("Batman"); ==> will return -3
-3 means that first string is short of argument string by 3 characters thus it shoud come before i.e
"Bat" < "Batman"
if you compare:
"Batman".compareTo("Bat");
it will return 3 i.e first string greater than second by 3 characters.
Precedence:
When comparing strings lexicographically following precedence takes place,
digits < Uppercase letters < Lowercase letters
compareTo() only compares contents not references:
compareTo() only compares caharacters of two strings not the references of string objects. This can be seen from following example,
String s="test";
String s1= new String("test");
Here s and s1 are two different String objects with equal contents.
s.compareTo(s1); will give 0 which means contents of s are same as of s1. Thus here only contents are measured not the references contained in s and s1. This case is same as of using s.equals(s1)
Some Examples:
1)
String s="Tiger";
String s1="Rider"
s.compareTo(s1); ==> will return 2
Since return value > 0 therefore Rider < Tiger.
2)
"a".compareTo("aa") ==> will return -1 which is < 0. Therefore
a < aa
3) "111".compareTo("11"); ==> will return 1 which is > 0, therefore
11 < 111
4) "a1".compareTo("aa") => will return -48 which is < 0 , therefore
a1< aa
5) "a".compareTo("A") ==> will return 32 whcich is > 0 therefore,
A < a
6) "Cat".compareTo("cat") => will return -32 which is < 0 , therefore
Cat < cat
7) "a".compareTo("2") ==> will return 47 whcich is > 0 therefore,
2 < a
8) "10".compareTo("11") ==> will return -1 which is < 0 therefore,
10 < 11
5) By using compareToIgnoreCase()
compareToIgnoreCase(), like equalsIgnoreCase() ignores the case while comparing the strings. Example,
"CAT".compareToIgnoreCase("cat")
will return 0 i.e both strings are lexicographically equal, while
"CAT".compareTo("cat") will return -32 i.e CAT < cat.
Note that ==, equals() and equalsIgnoreCase() returns boolean value while compareTo and compareToIgnoreCase() returns an int value.
1) Using == operator.
2) Using equals()
3) Using equalsIgnoreCase()
4) Using compareTo()
5) Using compareToIgnoreCase(String str)
1) By using == operator
When you compare two strings using ==, it only compares reference variables. It only checks whether the two reference variables are pointing to same String objects or not.
== DO NOT compare the CONTENTS of Strings. Often beginers do mistake of using == to compare strings for checking the equality of their contents. So never use == when you are comparing strings to match their contents as explained in the article String equality Check: equals() or == ?.
2) By using equals()
equals(), unlike ==, is used to compare the contents of two Strings. equals() compare two strings by doing character by character comparison and does case sensitive comparison.
3) By using equalsIgnoreCase()
equalsIgnoreCase() is same as equals() except that it ignores the case when comparing contents of strings.
So, "test".equalsIgnoreCase("TEST") will be true but for equals() it would be false:
"test".equals("TEST") // false.
equals() and equalsIgnoreCase() are described here.
4) By using compareTo()
compareTo() compares two strings lexicographically. This means compareTo() not only tells whether two strings have equal contents, but it also tells whether one string comes before or after the other string. This helps in situations where you need to sort the strings either in ascending or descending order.
Lexicographical order of strings is the same order in which words are arranged in a Dictionary.
String class overrides the compareTo() method from Comparable interface which it implements.
Actually while using compareTo() method the intention is not to check the equality of string data but to know that whether one string is less than or equal to or greater than other.
Suppose you are comparing two strings s and s1.Then,
i) If s.compareTo(s1) < 0
This implies that first string (s) should come before the argument string (s1).
Therefore s< s1.
ii) If s.compareTo(s1) == 0
Implies that first string (s) should is equal to argument string (s1) i.e same length and same contents.
iii) If s.compareTo(s1) > 0
This implies that first string (s) should come after the argument string (s1).
Therefore s1 < s.
According to JavaDocs while comparing strings lexicographically two cases can arise:
1) At least one character in two strings is different:
example,
"Rose".compareTo("Road");
In such case compareTo() will determine the position of first character which is different in two strings. Here it comes out at index 2 i.e character 's' and 'a'.
compareTo() then return the difference of these two characters from which you guess which string is smaller.
Here 's'-'a' comes out to be 18 which is the difference of their ascii values.
Since it is 18 > 0 first string is greater than argument string, therefore "Road" should come after "Rose" (Rose < Road).
2) Characters are same but one string is smaller in length than other:
Example,
"Bat".compareTo("Batman");
In this case there is no such first character which is different as "Bat" includes in "Batman". But "Bat" is smaller than "Batman" in length. Here compareTo() will return the difference of their lengths instead of the difference between their characters.
"Bat".compareTo("Batman"); ==> will return -3
-3 means that first string is short of argument string by 3 characters thus it shoud come before i.e
"Bat" < "Batman"
if you compare:
"Batman".compareTo("Bat");
it will return 3 i.e first string greater than second by 3 characters.
Precedence:
When comparing strings lexicographically following precedence takes place,
digits < Uppercase letters < Lowercase letters
compareTo() only compares contents not references:
compareTo() only compares caharacters of two strings not the references of string objects. This can be seen from following example,
String s="test";
String s1= new String("test");
Here s and s1 are two different String objects with equal contents.
s.compareTo(s1); will give 0 which means contents of s are same as of s1. Thus here only contents are measured not the references contained in s and s1. This case is same as of using s.equals(s1)
Some Examples:
1)
String s="Tiger";
String s1="Rider"
s.compareTo(s1); ==> will return 2
Since return value > 0 therefore Rider < Tiger.
2)
"a".compareTo("aa") ==> will return -1 which is < 0. Therefore
a < aa
3) "111".compareTo("11"); ==> will return 1 which is > 0, therefore
11 < 111
4) "a1".compareTo("aa") => will return -48 which is < 0 , therefore
a1< aa
5) "a".compareTo("A") ==> will return 32 whcich is > 0 therefore,
A < a
6) "Cat".compareTo("cat") => will return -32 which is < 0 , therefore
Cat < cat
7) "a".compareTo("2") ==> will return 47 whcich is > 0 therefore,
2 < a
8) "10".compareTo("11") ==> will return -1 which is < 0 therefore,
10 < 11
5) By using compareToIgnoreCase()
compareToIgnoreCase(), like equalsIgnoreCase() ignores the case while comparing the strings. Example,
"CAT".compareToIgnoreCase("cat")
will return 0 i.e both strings are lexicographically equal, while
"CAT".compareTo("cat") will return -32 i.e CAT < cat.
Note that ==, equals() and equalsIgnoreCase() returns boolean value while compareTo and compareToIgnoreCase() returns an int value.
I would like to know your comments and if you liked the article then please share it on social networking buttons.
No comments:
Post a Comment