Java: String compareTo(String anotherString) method

In this article, we will discuss how to compare two strings using String’s compareTo() method

 

compareTo(String anotherString) method: This String method compares 2 string lexicographically and returns an integer value

  • Returns 0, if str1 == str2 (both strings are lexicographically equal)
  • Returns +ve value, if str1 > str2 (1st string lexicographically greater than 2nd string)
  • Returns -ve value, if str1 < str2 (1st string lexicographically lesser than 2nd string)

 

Method Signature:

public int compareTo(String anotherString);

 

Returns:

  • Returns an integer value after lexicographical comparison

 

Examples on compareTo() method:

Below piece of code depicts lexicographical comparison

StringCompareToMethod.java

package in.bench.resources.string.methods;

public class StringCompareToMethod {

	public static void main(String[] args) {

		String str1 = "Star";  
		String str2 = "Star";
		String str3 = "OneStar";
		String str4 = "StarOne";

		// compare 1: for lexicographically equal value
		int compare1 = str1.compareTo(str2);

		// printing to console
		System.out.println("String comparison of str1 and str2 : " 
                                                           + compare1);

		// compare 2: for lexicographically +ve value
		int compare2 = str1.compareTo(str3);

		// printing to console
		System.out.println("String comparison of str1 and str3 : " 
                                                           + compare2);

		// compare 3: for lexicographically -ve value
		int compare3 = str1.compareTo(str4);

		// printing to console
		System.out.println("String comparison of str1 and str4 : " 
                                                            + compare3);
	}
}

Output:

String comparison of str1 and str2 : 0
String comparison of str1 and str3 : 4
String comparison of str1 and str4 : -3

 

References:

https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html
https://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html
https://docs.oracle.com/javase/tutorial/java/data/strings.html
https://docs.oracle.com/javase/6/docs/api/java/lang/String.html
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
https://docs.oracle.com/javase/6/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/7/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/8/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/6/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/6/docs/api/java/lang/StringBuilder.html
https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html
https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html

 

Happy Coding !!
Happy Learning !!

Java: String compareToIgnoreCase(String anotherString) method
Java: String charAt(int index) method