Java String compareTo()

compareTo() method is used to perform natural sorting on string. Natural sorting means the sort order which applies on the object, e.g., lexical order for String, numeric order for Sorting integers, etc.

Lexical order is nothing but alphabetically order. compareTo methods does a sequential comparison of letters in the string that have the same position.compareTo is defined in interface java.lang.Comparable

Syntax :
public int compareTo(String str)
Parameter input :

str – This method only accepts only one input String data type.

Method Returns:

This Java method returns an int datatype which is based on the lexicographical comparison between two strings.

  1. returns < 0 then the String calling the method is lexicographically first

  2. returns == 0 then the two strings are lexicographically equivalent

  3. returns > 0 then the parameter passed to the compareTo method is lexicographically first.

If first string is lexicographically greater than second string, it returns positive number (difference of character value). If first string is less than second string lexicographically, it returns negative number and if first string is lexicographically equal to second string, it returns 0.

if s1 > s2, it returns positive number if s1 < s2, it returns negative number if s1 == s2, it returns 0
Example:
public class Test1 { public static void main(String[] args) { String str_Sample = "a"; System.out.println("Compare To 'a' b is : " + str_Sample.compareTo("b")); str_Sample = "b"; System.out.println("Compare To 'b' a is : " + str_Sample.compareTo("a")); str_Sample = "b"; System.out.println("Compare To 'b' b is : " + str_Sample.compareTo("b")); } }


Output:
Compare To 'a' b is : -1 Compare To 'b' a is : 1 Compare To 'b' b is : 0
  1. Character a comes before b alphabetically. Hence output is -1

  2. Character b comes before a alphabetically. Hence output is 1

  3. Character b are equivalent, hence output is 0.

Another Example
public class Test { public static void main(String[] args) { //Compare to a String String str_Sample = "RockStar"; System.out.println("Compare To 'ROCKSTAR': " + str_Sample.compareTo("rockstar")); //Compare to - Ignore case System.out.println("Compare To 'ROCKSTAR' - Case Ignored: " + str_Sample.compareToIgnoreCase("ROCKSTAR")); } }


Output:
Compare To 'ROCKSTAR': -32 Compare To 'ROCKSTAR' - Case Ignored: 0
Internal implementation
public boolean equals(Object anObject) { if (this == anObject) { return true; } if (anObject instanceof String) { String anotherString = (String) anObject; int n = value.length; if (n == anotherString.value.length) { char v1[] = value; char v2[] = anotherString.value; int i = 0; while (n-- != 0) { if (v1[i] != v2[i]) return false; i++; } return true; } } return false; }


Signature
public int compareTo(String anotherString)
Parameters

anotherString: represents string that is to be compared with current string

Returns

an integer value

Java String compareTo() method example
public class Test { public static void main(String args[]) { String s1="rama"; String s2="rama"; String s3="pavi"; String s4="pavi"; String s5="flag"; System.out.println(s1.compareTo(s2)); System.out.println(s1.compareTo(s3)); System.out.println(s1.compareTo(s4)); System.out.println(s1.compareTo(s5)); } }


Output:
0 2 2 12
Java String compareTo(): empty string

If you compare string with blank or empty string, it returns length of the string. If second string is empty, result would be positive. If first string is empty, result would be negative.

public class Test { public static void main(String args[]) { String s1="hello"; String s2=""; String s3="me"; System.out.println(s1.compareTo(s2)); System.out.println(s2.compareTo(s3)); } }


Output:
5 -2
When to use CompareTo() method?

CompareTo() is used for comparing two strings lexicographically. Each character of both strings are converted into a Unicode value. However, if both the strings are equal, then this method returns 0 else it only result either negative or positive value.

In this method, if the first string is always lexicographically higher than second string, it returns a positive number.

if a1 > a2, it returns negative number if a1 < a2, it returns positive number if a1 == a2, it returns 0
Example:
public class Test { public static void main(String[] args) { String s1 = "Rama"; String s2 = "Pavi"; System.out.println("String 1: " + s1); System.out.println("String 2: " + s2); // Compare the two strings. int S = s1.compareTo(s2); // Show the results of the comparison. if (S < 0) { System.out.println("\"" + s1 + "\"" + " is lexicographically higher than " + "\"" + s2 + "\""); } else if (S == 0) { System.out.println("\"" + s1 + "\"" + " is lexicographically equal to " + "\"" + s2 + "\""); } else if (S > 0) { System.out.println("\"" + s1 + "\"" + " is lexicographically less than " + "\"" + s2 + "\""); } } }


Output:
String 1: Rama String 2: Pavi "Rama" is lexicographically less than "Pavi"



Instagram