Method: Java String compareTo()
This method is basically used to compare two strings that may be different or the same. It compares both the strings lexicographically.
A Lexicographical comparison compares each character of both the strings converted into their Unicode values for comparison. If the strings are equal (difference of Unicode values) then it returns 0 (zero) and if the first string is lexicographically greater than the second string then it returns a positive value otherwise a negative value.

- Java String compareTo(String str).
- Java String compareTo(Object obj).
The first one does the comparison of the two string literals.
String1.compareTo(String2) where String1 and String2 are string literals.
The second one does the comparison between a string and an object.
String1.compareTo(String object) where String1 is a literal whose value would be compared with the string specified in the method argument.
Let's go through the example.
Example
The following example shows outputs for both the variants.
- public class CompareTo
- {
- public static void main(String args[])
- {
- String str1="Apple is a fruit";
- String str2="Apple is also a brand";
- String str3="Apple is a fruit";
- System.out.println("After string to string comparison we get: "+str2.compareTo(str1));//positive
- System.out.println("After string to string comparison we get: "+str1.compareTo(str3));//zero
- System.out.println("After string to string comparison we get: "+str1.compareTo(str2));//negative
- System.out.println();
- System.out.println("After string to object comparison we get: "+str2.compareTo("Apple is a fruit"));//positive
- System.out.println("After string to object comparison we get: "+str1.compareTo("Apple is a fruit"));//zero
- System.out.println("After string to object comparison we get: "+str1.compareTo("Apple is also a brand"));//negative
- }
- }

Method: Java String compareToIgnoreCase()
This method is quite similar to the preceding method but with the difference that this method ignores the cases (uppercase and lowercase). It also compares lexicographically as does the preceding method and returns the same values for the preceding specified condition.
Example
In this example, we took three strings s1=lowercase, s2=uppercase, s3=mixedcase to compare these three strings.
- public class CompareTo
- {
- public static void main(String args[])
- {
- String str1="love programming";
- String str2="LOVE PROGRAMMING";
- String str3="Love Programming";
- System.out.println("After string to string comparison we get: "+str1.compareToIgnoreCase(str1));//equal strings hence returns zero
- System.out.println("After string to string comparison we get: "+str1.compareToIgnoreCase(str2));//ignores case sensitivity hence returns zero
- System.out.println("After string to string comparison we get: "+str1.compareToIgnoreCase(str3));//ignores case sensitivity hence returns zero
- System.out.println();
- System.out.println("After string to object comparison we get: "+str1.compareToIgnoreCase("love programming"));//equal strings hence returns zero
- System.out.println("After string to object comparison we get: "+str1.compareToIgnoreCase("LOVE PROGRAMMING"));//ignores case sensitivity hence returns zero
- System.out.println("After string to object comparison we get: "+str1.compareToIgnoreCase("Love Programming"));//ignores case sensitivity hence returns zero
- }
- }




Santhakumar MunuswamyPosted May 4, 2015, 2:23 PM
Thanks for nice article
Karthik Muthu KaruppanPosted May 4, 2015, 1:55 PM
good
Abhishek JaiswalPosted May 4, 2015, 11:03 AM
Keep going. very helpful for JAVA developers! :)