Java String compareTo() Method


Java String compareTo() Method with examples

The Java String compareTo() method is used for comparing two strings lexicographically. Each character of both the strings is converted into a Unicode value for comparison. If both the strings are equal then this method returns 0 else it returns positive or negative value. The result is positive if the first string is lexicographically greater than the second string else the result would be negative.

Java String compareTo() method

We have following two ways to use compareTo() method:

int compareTo(String str)

Here the comparison is between string literals. For example string1.compareTo(string2) where string1 and string2 are String literals.

int compareTo(Object obj)

Here the comparison is between a string and an object. For example string1.compareTo("Just a String object") where string1 is a literal and it’s value is compared with the string specified in the method argument.

ava String compareTo() method Example

Here we have three Strings and we are comparing them with each other using compareTo() method.

public class CompareToExample {
   public static void main(String args[]) {
       String str1 = "String method tutorial";
       String str2 = "compareTo method example";
       String str3 = "String method tutorial";

       int var1 = str1.compareTo( str2 );
       System.out.println("str1 & str2 comparison: "+var1);

       int var2 = str1.compareTo( str3 );
       System.out.println("str1 & str3 comparison: "+var2);

       int var3 = str2.compareTo("compareTo method example");
       System.out.println("str2 & string argument comparison: "+var3);


str1 & str2 comparison: -16
str1 & str3 comparison: 0
str2 & string argument comparison: 0

How to find length of a string using String compareTo() method

Here we will see a fascinating illustration of how to utilize the compareTo() technique to discover the length of a string. In the event that we contrast a string and an unfilled string utilizing the compareTo() technique then the strategy would restore the length of the non-void string.

For example:

String str1 = "Negan";  
String str2 = ""; //empty string

//it would return the length of str1 in positive number
str1.compareTo(str2); // 5

//it would return the length of str1 in negative number
str2.compareTo(str1); //-5

In the above code scrap, the second compareTo() articulation restored the length in negative number, this is on the grounds that we have contrasted the vacant string and str1 while in first compareTo() proclamation, we have contrasted str1 and void string.

Lets see the complete example:

public class JavaExample {
    public static void main(String args[]) {
	String str1 = "Cow"; 
	//This is an empty string
	String str2 = "";
	String str3 = "Goat";


Java String compareTo() method example

Is Java String compareTo() method case sensitive?

In this example, we will compare two strings using compareTo() method. Both the strings are same, however one of the string is in uppercase and the other string is in lowercase.

public class JavaExample {
   public static void main(String args[]) {
	String str1 = "HELLO"; 
	String str2 = "hello";;

Java String compareTo() case sensitive

As you can see that the output is not zero, which means that compareTo() method is case sensitive. However we do have a case insensitive compare method in string class, which is compareToIgnoreCase(), this method ignores the case while comparing two strings.