Java Program example to demonstrate String compareTo method
Program
public class StringCompareTo
{
	public static void main(String[] args)
	{
		String str1 = "Welcome to oodlescoop";
		String str2 = "Welcome to oodlescoop";
		String str3 = "Hello all";
		String str4 = "Welcome to oodlescoop tutorials";
		String str5 = "WELCOME TO OODLESCOOP";
		System.out.println(str1.compareTo(str2) + "\t: strings are equal");
		System.out.println(str1.compareTo(str3) + "\t: first string is greater");
		System.out.println(str1.compareTo(str4) + "\t: second string is greater");
		System.out.println(str1.compareTo(str5) + "\t: unequal due to case of the text");
	}
}
This Java program demonstrates the use of the compareTo method in the String class to compare two strings lexicographically.
1. Class Definition
public class StringCompareTo
- Defines the class StringCompareTo.
2. Main Method
public static void main(String[] args)
- Entry point for the program.
3. Define Strings
String str1 = "Welcome to oodlescoop";
String str2 = "Welcome to oodlescoop";
String str3 = "Hello all";
String str4 = "Welcome to oodlescoop tutorials";
String str5 = "WELCOME TO OODLESCOOP";
- str1and- str2: Identical strings.
- str3: A string that is lexicographically smaller than- str1.
- str4: A string that is lexicographically larger than- str1.
- str5: Same as- str1but with different case.
4. Compare Strings
System.out.println(str1.compareTo(str2) + "\t: strings are equal");
System.out.println(str1.compareTo(str3) + "\t: first string is greater");
System.out.println(str1.compareTo(str4) + "\t: second string is greater");
System.out.println(str1.compareTo(str5) + "\t: unequal due to case of the text");
- compareToMethod:- Compares two strings lexicographically.
- Returns:
- 0: If the two strings are equal.
- A positive number: If the first string is lexicographically greater than the second.
- A negative number: If the first string is lexicographically smaller than the second.
 
- Case-sensitive comparison.
 
1. Compare str1 and str2
str1.compareTo(str2)
- Both strings are identical.
- Result: 0(strings are equal).
- Output:
0 : strings are equal
2. Compare str1 and str3
str1.compareTo(str3)
- str1:- "Welcome to oodlescoop".
- str3:- "Hello all".
- Lexicographically, "Welcome to oodlescoop"is greater because'W'comes after'H'in ASCII.
- Result: A positive number.
- Output:
15 : first string is greater
3. Compare str1 and str4
str1.compareTo(str4)
- str1:- "Welcome to oodlescoop".
- str4:- "Welcome to oodlescoop tutorials".
- Lexicographically, str1is smaller becausestr4has additional characters.
- Result: A negative number.
- Output:
-9 : second string is greater
4. Compare str1 and str5
str1.compareTo(str5)
- str1:- "Welcome to oodlescoop".
- str5:- "WELCOME TO OODLESCOOP".
- Lexicographically, uppercase letters come before lowercase letters in ASCII.
- Result: A positive number.
- Output:
32 : unequal due to case of the text
Key Points About compareTo
- 
Lexicographical Order: - Comparison is based on the Unicode values of characters.
- Characters are compared one by one until a difference is found or one string ends.
 
- 
Case Sensitivity: - compareTois case-sensitive:- Uppercase letters (A-Z) have smaller Unicode values than lowercase letters (a-z).
 
- Uppercase letters (
 
- 
Length of Strings: - If two strings are identical up to the length of the shorter string, the longer string is considered greater.
 
Output
0	: strings are equal
15	: first string is greater
-10	: second string is greater
32	: unequal due to case of the text