Java String.compareTo() Method

Last Updated : 17 Sept 2026

In Java, the java.lang.String class provides the compareTo() method to compare two strings lexicographically (in dictionary order). This comparison is based on the Unicode value of characters present in the string.

The String class provides the following two methods to compare String:

MethodDescription
compareTo(String anotherString)The method compares two strings lexicographically.
compareToIgnoreCase(String str)The method compares two strings lexicographically, ignoring case differences.

String.compareTo() Method

The method to compare two strings lexicographically. It returns 0 exactly when the String.equals(Object) method would return true.

Syntax:

public int compareTo(String anotherString)

Parameters: The method accepts a string to be compared as a parameter.

Returns:

  • The method returns 0 if the argument string is equal to this string.
  • A value less than 0 if this string is lexicographically less than the string argument.
  • A value greater than 0 if this string is lexicographically greater than the string argument.

Internal Implementation

Example: Comparing Strings

Java

Compile and Run

Output:

Comparing str1 and str2: -1
Comparing str2 and str3: 1
Comparing str1 and str3: 0

Explanation

In this example, "Apple" comes before "Banana", so str1.compareTo(str2) gives a negative value. "Apple" and "Apple" are equal, so their comparison gives 0.

Note: The String.compareTo() method considers the case sensitivity of characters present in a string.

Example

Compile and Run

Output:

Difference of the string 'TpointTech' and 'tpointTech' is: -32

Explanation

In the above program, the first string is “TpointTech” and the second string is “tpointTech”. When we compare both the strings each other, a mismatch occurs. The Unicode value of the character ‘T’ and ‘t’ is 84 and 116, respectively. Their difference is -32 (84 – 116 = -32). So, the output is -32.

For ignoring the case sensitivity of characters, the String class provides the String.compareToIgnoreCase() method.

String.compareToIgnoreCase() Method

This method compares two strings in dictionary order (lexicographically) by ignoring case sensitivity. The method first converts all the characters of the string in lowercase, and then compares the strings.

Syntax:

Parameters: The method accepts string to be compared as a parameter.

Returns: The method returns a negative integer, zero, or a positive integer as the specified String is greater than, equal to, or less than this String, ignoring case considerations.

Example: Comparing Strings Ignoring the Case Sensitivity

Java

Compile and Run

Output:

Comparing str1 and str2: -1
Comparing str2 and str3: 1
Comparing str1 and str3: 0

Explanation

The String.compareToIgnoreCase() method compares two strings lexicographically without considering uppercase and lowercase differences.

  • Negative value→ First string comes before the second string.
  • Positive value→ First string comes after the second string.
  • 0→ Both strings are equal, ignoring case.

For example, "Apple" and "APPLE" are considered equal, so their comparison returns 0.

Exceptions in String.compareTo() Method

The String.compareTo() method may raise the following two exceptions:

To read more Exception Handling in Java

ClassCastException

ClassCastException is a runtime exception that occurs only when objects of incompatible types are compared using the String.compareTo() method.

Example: Comparing Two Different Objects

Java

Output:

Runtime Error:
Exception in thread "main" java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.String (java.lang.Integer and java.lang.String are in module java.base of loader 'bootstrap')
	at Main.main(Main.java:6)

NullPointerException

The NullPointerException is raised if either of the objects that are getting compared is null. It makes sure that one must explicitly take care of the null values and stop this unexpected behaviour.

Example: Comparing String with Null Object

Java

Output:

Runtime Error:
Exception in thread "main" java.lang.NullPointerException
	at Main.main(Main.java:6)