Java String compareTo() Method

Java String compareTo() Method

In Java, comparing two strings is the most common task in situations like file validation, user authentication, form input processing, and database queries. The compareTo() method helps compare two strings based on their lexicographical order (dictionary order). 

In this blog, we will explore how to use the compareTo() method with examples.

Table of Contents:

What is String.compareTo() Method in Java?

The compareTo() method in Java is used to compare two strings lexicographically (dictionary order). It compares the ASCII values of characters in both strings. This method is case-sensitive, meaning uppercase and lowercase letters are treated differently.

Syntax:

int result = string1.compareTo(string2);

Parameters:

  • string1 is the first string to compare
  • string2 is the second string to compare
  • result is an integer either( 0, positive or negative).

Return Type: It returns an integer value 

  • Returns 0: If both strings are equal.
  • Negative value: If string1 is lexicographically less than string2.
  • Positive value: If string1 is lexicographically greater than string2.

Variants of CompareTo() Method

Here are the following three variants of the compareTo() method in Java:

  • Using int compareTo(Object obj)
  • Using int compareTo(String AnotherString)
  • Using int compareToIgnoreCase(String str) 

1. Using int compareTo(Object obj)

This method is used to compare a String with another Object. However, it is not commonly used because it requires explicit typecasting.

Syntax:

int result = string1.compareTo(object);

Parameters:

  • object: The object to be compared.

Example:

Java
public class Main {
public static void main(String[] args) {
String str1 = "Hello";
Object obj = "Hello";
int result = str1.compareTo((String) obj);
System.out.println("Comparison Result: " + result);
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Output:

Using int compareTo(Object obj)

Explanation: In the above code, we have compared a string and an object with the help of compareTo() method.

2. Using int compareTo(String anotherString)

This method compares two strings lexicographically based on their ASCII values.

Syntax:

int result = string1.compareTo(string2);

Parameters: string2 is the string to be compared.

Example:

Java
public class Main {
public static void main(String[] args) {
String str1 = "Intellipaat";
String str2 = "intellipaat";
int result = str1.compareTo(str2);
System.out.println("Comparison Result: " + result);
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Output:

Using int compareTo(String anotherString)

Explanation: In the above code, both the strings are not equal, the difference between ‘I’ and ’i’ is -32. So it returns -32.

3. Using int compareToIgnoreCase(String str)

This method is similar to compareTo(), but it ignores case differences while comparing.

Syntax:

int result = string1.compareToIgnoreCase(anotherString);

Example:

Java
public class Main {
public static void main(String[] args) {
String str1 = "hello";
String str2 = "HELLO";
int result = str1.compareToIgnoreCase(str2);
System.out.println("Comparison Result: " + result);
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Output:

Using int compareToIgnoreCase(String str)

Explanation: In the above code, even though one string is in lowercase and the other is in uppercase, compareToIgnoreCase() treats them as equal and returns 0.

Examples to Compare Strings using String.compareTo() Method in Java

Here are the examples to compare strings using String.compareTo() Method in Java:

Example1: Comparing Strings

Java
public class StringComparisonExample {
public static void main(String[] args) {
String str1 = "Apple";
String str2 = "Banana";
String str3 = "Apple";
int result1 = str1.compareTo(str2);
int result2 = str1.compareTo(str3);
int result3 = str2.compareTo(str1);
System.out.println("str1 compareTo str2: " + result1);
System.out.println("str1 compareTo str3: " + result2);
System.out.println("str2 compareTo str1: " + result3);
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Output:

Examples to Compare Strings using String.compareTo() Method in Java

Explanation: In the above code,

1. str1.compareTo(str2):  “Apple”.compareTo(“Banana”)

  • The method compares characters lexicographically.
  • ‘A’ (ASCII: 65) comes before ‘B’ (ASCII: 66).
  • Since “Apple” is less than “Banana”, the result is negative (-1 or another negative value).

2. str1.compareTo(str3): “Apple”.compareTo(“Apple”)

  • Both strings are identical.
  • The result is 0, which means both the strings are equal.

3. str2.compareTo(str1): “Banana”.compareTo(“Apple”)

  • ‘B’ (ASCII: 66) comes after ‘A’ (ASCII: 65).
  • Since “Banana” is greater than “Apple“, the result is positive (1 or another positive value).

Example 2: Check if Two Strings are Equal

In the following example, we will check if two strings are equal or not using String.compareTo() method:

Example:

Java
class Main {
public static void main(String[] args) {
String str1 = "Learn JavaScript";
String str2 = "Learn Java";
if (str1.compareTo(str2) == 0) {
System.out.println("str1 and str2 are equal");
}
else {
System.out.println("str1 and str2 are not equal");
}
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Output:

Check if Two Strings are Equal

Explanation: The compareTo() method compares “Learn JavaScript” and “Learn Java” lexicographically. Since “Learn JavaScript” is longer and comes after “Learn Java“, compareTo() returns a positive value, so the output will be “str1 and str2 are not equal“.

Example 3: compareTo() With Case

The compareTo() method in Java is case-sensitive, meaning it considers uppercase and lowercase letters as different based on their ASCII values.

Example:

Java
public class StringComparisonExample {
public static void main(String[] args) {
String str1 = "Java";
String str2 = "java";
int result = str1.compareTo(str2);
System.out.println("str1 compareTo str2: " + result);
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Output:

compareTo() With Case

Explanation: This program compares two strings, “Java” and “java“, using the compareTo() method. Since uppercase ‘J’ has a smaller ASCII value than lowercase ‘j’, the method returns -32, meaning “Java” comes before “java” in dictionary order.

Exceptions in Java String compareTo() Method

Below are the possible exceptions that may occur while using compareTo() method in Java:

1. NullPointerException

The compareTo() method throws a NullPointerException if the string is compared to null.

Example:

Java
public class Main {
public static void main(String[] args) {
String str1 = "Java";
String str2 = null;
int result = str1.compareTo(str2);
System.out.println("Comparison Result: " + result);
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Output:

NullPointerException

Explanation: Since str2 is null, calling compareTo() on str1 leads to a NullPointerException.

2. compareTo() ClassCastException

The compareTo() method can throw a ClassCastException when the object is not a String.

Example:

Java
public class Main {
public static void main(String[] args) {
String str1 = "Java";
Object str2 = 100;
int result = str1.compareTo((String) str2);
System.out.println("Comparison Result: " + result);
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Output:

compareTo() ClassCastException

Explanation: Here, str2 is an Integer, so explicitly changing it to String will lead to ClassCastException.

Conclusion

In this blog, we have learned how to compare strings using the compareTo() method in Java. We also explored its different versions with examples and also explored possible exceptions that can occur while using this method.

If you want to learn more about Java, you may refer to our Java Course.

Some Other Methods to Compare Strings in Java

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.

Full Stack Developer Course Banner