Lexicographical comparison means comparing two strings based on the order of their characters, similar to how words are arranged in a dictionary. Java provides the compareTo() and compareToIgnoreCase() methods to compare strings lexicographically.
- compareTo() compares two strings lexicographically and is case-sensitive.
- compareToIgnoreCase() compares two strings lexicographically while ignoring case.
- A negative value means the first string comes before the second string.
- 0 means both strings are equal lexicographically.
- A positive value means the first string comes after the second string.
public class CompareStrings{
public static void main(String[] args) {
String s1 = "Java";
String s2 = "Python";
int result = s1.compareTo(s2);
System.out.println(result);
}
}
Output
-6
Explanation: The compareTo() method compares the strings character by character. The first different characters are 'J' and 'P'. Their Unicode values are 74 and 80, respectively.
Therefore:
'J' - 'P' = 74 - 80 = -6
Since the result is negative, "Java" comes before "Python" lexicographically.
Note: The exact positive or negative value returned by compareTo() should generally not be relied upon. What matters is whether the result is negative, zero, or positive.
Methods to Compare Two Strings Lexicographically
1. Using String.compareTo()
The String.compareTo() method is the standard way to compare two strings lexicographically in Java. It returns:
- a negative value if the first string comes before the second string.
- 0 if both strings are equal.
- a positive value if the first string comes after the second string.
public class CompareStrings{
public static void main(String[] args) {
String s1 = "Apple";
String s2 = "Banana";
int result = s1.compareTo(s2);
if (result < 0) {
System.out.println(s1 + " comes before " + s2);
} else if (result > 0) {
System.out.println(s1 + " comes after " + s2);
} else {
System.out.println("Both strings are equal");
}
}
}
Output
Apple comes before Banana
Explanation: The first characters of the strings are 'A' and 'B'. Since 'A' comes before 'B', the result of compareTo() is negative.
2. Comparing Equal Strings
If two strings contain the same characters in the same order, compareTo() returns 0.
public class CompareStrings{
public static void main(String[] args){
String s1 = "Java";
String s2 = "Java";
System.out.println(s1.compareTo(s2));
}
}
Output
0
Explanation: Both strings have identical content, so they are equal lexicographically.
3. Using String.compareToIgnoreCase()
The compareToIgnoreCase() method performs a lexicographical comparison while ignoring differences in letter case.
public class CompareStrings {
public static void main(String[] args) {
String s1 = "Java";
String s2 = "JAVA";
int result = s1.compareToIgnoreCase(s2);
System.out.println(result);
}
}
Output
0
Explanation: Although "Java" and "JAVA" have different cases, compareToIgnoreCase() ignores those differences. Therefore, both strings are considered equal lexicographically.
4. Comparing Strings with Different Prefixes
When one string is a prefix of another, the shorter string is considered smaller.
public class CompareStrings {
public static void main(String[] args) {
String s1 = "Java";
String s2 = "JavaScript";
int result = s1.compareTo(s2);
if (result < 0) {
System.out.println(s1 + " comes before " + s2);
} else if (result > 0) {
System.out.println(s1 + " comes after " + s2);
} else {
System.out.println("Both strings are equal");
}
}
}
Output
Java comes before JavaScript
Explanation: The characters "Java" are common in both strings. Since "Java" ends before "JavaScript", the shorter string comes first lexicographically.
5. Comparing Strings Using if-else
The result of compareTo() is commonly used with conditional statements.
public class CompareStrings {
public static void main(String[] args) {
String s1 = "Geeks";
String s2 = "Java";
int result = s1.compareTo(s2);
if (result < 0) {
System.out.println("s1 comes before s2");
} else if (result > 0) {
System.out.println("s1 comes after s2");
} else {
System.out.println("s1 and s2 are equal");
}
}
}
Output
s1 comes before s2
Explanation: The first characters are 'G' and 'J'. Since 'G' comes before 'J', compareTo() returns a negative value.
compareTo() vs compareToIgnoreCase()
| compareTo() | compareToIgnoreCase() |
|---|---|
| Performs case-sensitive comparison. | Performs case-insensitive comparison. |
| Uppercase and lowercase letters are treated as different. | Uppercase and lowercase letters are treated as equivalent. |
| Example: "Java".compareTo("java") gives a non-zero result. | Example: "Java".compareToIgnoreCase("java") returns 0. |
| Used when letter case matters during comparison. | Used when letter case should be ignored during comparison. |