The compareTo()
method compares two strings lexicographically (in the dictionary order). The comparison is based on the Unicode value of each character in the strings.
Example
class Main {
public static void main(String[] args) {
String str1 = "Learn Java";
String str2 = "Learn Kolin";
int result;
// comparing str1 with str2
result = str1.compareTo(str2);
System.out.println(result);
}
}
// Output: -1
Syntax of compareTo()
The syntax of the compareTo()
method is:
string.compareTo(String str)
Here, string is an object of the String
class.
compareTo() Parameters
The compareTo()
method takes a single parameter.
- str - the string to be compared
compareTo() Return Value
- returns 0 if the strings are equal
- returns a negative integer if the
string
comes before thestr
argument in the dictionary order - returns a positive integer if the
string
comes after thestr
argument in the dictionary order
Example: Java String compareTo()
class Main {
public static void main(String[] args) {
String str1 = "Learn Java";
String str2 = "Learn Java";
String str3 = "Learn Kolin";
int result;
// comparing str1 with str2
result = str1.compareTo(str2);
System.out.println(result); // 0
// comparing str1 with str3
result = str1.compareTo(str3);
System.out.println(result); // -1
// comparing str3 with str1
result = str3.compareTo(str1);
System.out.println(result); // 1
}
}
Here,
- str1 and str2 are equal. Hence,
str1.compareTo(str2)
returns 0. - str1 comes before str3 in the dictionary order. Hence,
str1.compareTo(str3)
returns negative, andstr3.compareTo(str1)
returns positive.
Example 2: Check if Two Strings are Equal
class Main {
public static void main(String[] args) {
String str1 = "Learn Python";
String str2 = "Learn Java";
// if str1 and str2 are equal, the result is 0
if (str1.compareTo(str2) == 0) {
System.out.println("str1 and str2 are equal");
}
else {
System.out.println("str1 and str2 are not equal");
}
}
}
Output
str1 and str2 are not equal
Example 3: compareTo() With Case
The compareTo()
method takes the letter case (uppercase and lowercase) into consideration.
class Main {
public static void main(String[] args) {
String str1 = "Learn Java";
String str2 = "learn Java";
int result;
// comparing str1 with str2
result = str1.compareTo(str2);
System.out.println(result); // -32
}
}
When "Learn Java"
is compared to "learn Java"
, we do not get 0. It is because compareTo()
takes the letter case into consideration.
Notes:
- If you need to compare two strings ignoring case differences, use the Java String compareToIgnoreCase() method.
- If you pass
null
to thecompareTo()
method, you will get an error.
Related Tutorial: Java String equals()