Java String compareTo

The Java String compareTo method is one of the String Methods, which is to compare the string with user-specified string lexicographically. The comparison based on the Unicode’s of the string characters.

In this article, we will show how to write String compareTo in Java Programming language with example. The syntax of the String compareTo in Java Programming language is as shown below.

Java String compareTo Syntax

The following Java compareto method will accept the String data as an argument and compare both the strings to check whether they are lexicographically equal or not.

public int compareTo(String Str); // It will return integer 

//In order to use in program
String_Object.compareTo(String Str)

The String.compareTo method will lexicographically compare the character sequence of String_Object with the character sequence of Str.

  • If the String_Object lexicographically precedes the string Str String.compareTo will return the result as Negative Integer.
  • If the String_Object lexicographically follows the string Str, String.compareTo will return the result as Positive Integer.
  • When the String_Object and the string Str are equal, compareto will return the result as Zero.

String compareTo Example

The Java compareTo method is to compare the string and user-specified string data lexicographically. In this program, We are going to do the same.

// Java String.compareTo example
package StringFunctions;

public class CompareTo {
	public static void main(String[] args) {
		String str = "Java Programming";
		String str1 = "Java Programming";
		String str2 = "Java";
		String str3 = "Java Programming Language";
		
		int a = str.compareTo(str1);
		int b = str.compareTo(str2);
		int c = str.compareTo(str3);
		int d = str2.compareTo("Java");
		int e = str2.compareTo("JAVA");

		System.out.println("Comparing the String str and str1 = " + a);
		System.out.println("Comparing the String str and str2 = " + b);
		System.out.println("Comparing the String str and str3 = " + c);
		System.out.println("Comparing the String str2 and Literal = " + d);
		System.out.println("Comparing the String str2 and Literal = " + e);
	}
}
Java String compareTo Method 1

It will call the public int compareTo (String str) method to compare the string str with str2. From the above screenshot, please observe that the Java compareto function is returning Zero because they both are lexicographical equal.

int a = str.compareTo(str1);

The following Java statement will compare str with str2. From the above screenshot, it is returning 12 because str has extra 12 character values.

int b = str.compareTo(str2);

The following String Method statement will compare str with str3. From the above screenshot, it is returning -9 because str3 has extra 9 character values compared to str.

int c = str.compareTo(str3);

Next, we assigned a string directly inside our String.compareTo method. It is returning 32 because the Unicode values of Uppercase & Lowercase letters are different.

int e = str2.compareTo("JAVA");