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");
	}
}

Output

0	: strings are equal
15	: first string is greater
-10	: second string is greater
32	: unequal due to case of the text

Tags: