In this tutorial, you will learn about String compareTo() method, its syntax, and usage with examples.
Java String compareTo() method
In Java, String compareTo() method takes a string value as argument, compares the string and given string lexicographically, and returns an integer value.
- The return value is 0, if the string and given string are equal.
- The return value is less than 0, if the string is lexicographically less than the argument string.
- The return value is greater than 0, if the string is lexicographically greater than the argument string.
Syntax of compareTo()
The syntax to call String compareTo() method in Java is
string1.compareTo(string2)
compareTo() has a single parameter.
Parameter | Description |
---|---|
string2 | [Mandatory] A string value. This string value is used for comparison with the string1 . |
compareTo() returns value of int type.
Examples
1. compareTo() – When both the strings are equal
In this example, we take two strings: string1 and string2; and lexicographically compare these two strings using String compareTo() method. We have taken the string values such that they are equal in value.
Java Program
public class Main {
public static void main(String[] args) {
String string1 = "apple";
String string2 = "apple";
int comparison = string1.compareTo(string2);
if (comparison == 0) {
System.out.println("The two strings are EQUAL.");
} else if (comparison < 0) {
System.out.println("string1 is LESS THAN string2.");
} else {
System.out.println("string1 is GREATER THAN string2.");
}
}
}
Output
The two strings are EQUAL.
2. compareTo() – When string1 is less than string2
In this example, we take two strings: string1 and string2; such that string1 is lexicographically less than string2, and compare them using String compareTo() method.
Java Program
public class Main {
public static void main(String[] args) {
String string1 = "apple";
String string2 = "banana";
int comparison = string1.compareTo(string2);
if (comparison == 0) {
System.out.println("The two strings are EQUAL.");
} else if (comparison < 0) {
System.out.println("string1 is LESS THAN string2.");
} else {
System.out.println("string1 is GREATER THAN string2.");
}
}
}
Output
string1 is LESS THAN string2.
3. compareTo() – When string1 is greater than string2
In this example, we take two strings: string1 and string2; such that string1 is lexicographically greater than string2, and compare them using String compareTo() method.
Java Program
public class Main {
public static void main(String[] args) {
String string1 = "cherry";
String string2 = "apple";
int comparison = string1.compareTo(string2);
if (comparison == 0) {
System.out.println("The two strings are EQUAL.");
} else if (comparison < 0) {
System.out.println("string1 is LESS THAN string2.");
} else {
System.out.println("string1 is GREATER THAN string2.");
}
}
}
Output
string1 is GREATER THAN string2.
Conclusion
In this Java String Methods tutorial, we have seen about String compareTo() method in Java, its syntax, and how to use String compareTo() method in Java programs with the help of examples.