Java programming language provides the compareTo()
function which is used to compare two strings. The comparison is done letter by letter and according to the situation, some results are returned by the compareTo()
function. The comparison is done with the Unicode character set.
compareTo() Syntax
First, we will learn the syntax of the compareTo()
function as the return type, parameters, etc. compareTo() function has two main syntaxes where one accepts a string parameter and the other accepts an object type parameter.
int compareTo(String str)
- Here the return type fo the compareTo() function is
int
which will provide the comparison result as an integer. - There is an only a single string which will be compared with the compareTo() function object like
text.compareTo(myword)
int compareTo(Object obj)
- Here the return type fo the compareTo() function is
int
which will provide the comparison result as an integer. - There is only a single object type parameter which will be converted to the string and compared with the compareTo() function like
myobj.compareTo(yourobj)
compareTo() Return Values
The most important part of the compareTo() function is the returned result. As we have learned compareTo() function returns an int or integer type value which expresses the comparison result. Let’s look following code which results will be like below.
int result = myfirststring.compareTo(mysecondstring);
- If
result
is 0 both strings are the same. - If
result
is bigger than 0myfirststring
is bigger thanmysecondstring
. - If
result
is lower than 0mysecondstring
is bigger thanmyfirststring
.
Compare with Strings
Let’s work with the compareTo() function with different strings in examples. In these examples, we will compare the strings ABC
, ABD
, ACD
with each other.
//CompareTo Examples In Java Programming Language import java.util.*; public class CompareTo_Example { public static void main(String[] args) { String s1="ABC"; String s2="ABD"; String s3="ACD"; String s4="AAA"; String s5="BBB"; System.out.println(s1+" Compare To "+s1+" and Result is "+s1.compareTo(s1)); System.out.println(s1+" Compare To "+s2+" and Result is "+s2.compareTo(s1)); System.out.println(s1+" Compare To "+s3+" and Result is "+s3.compareTo(s1)); System.out.println(s1+" Compare To "+s4+" and Result is "+s4.compareTo(s1)); System.out.println(s1+" Compare To "+s5+" and Result is "+s5.compareTo(s1)); } }

We will compile with the javac
and run the Java byte code like below.

compareTo() Function Is Case Sensitive
While comparing the string case sensitivity is very important. Case sensitive comparison will result in different for the same letter upper case and lower case like A and a. compareTo() function is also case sensitive by default because each letter has a different number for Unicode. We can use compareToIgnoreCase() function if we want to compare it in case-sensitively.
//CompareTo Examples In Java Programming Language import java.util.*; public class CompareTo_Example { public static void main(String[] args) { String s1="ABC"; String s2="abc"; String s3="Abc"; System.out.println(s1+" Compare To "+s1+" and Result is "+s1.compareTo(s1)); System.out.println(s1+" Compare To "+s2+" and Result is "+s2.compareTo(s1)); System.out.println(s1+" Compare To "+s3+" and Result is "+s3.compareTo(s1)); } }

compareToIgnoreCase() Function
If we want to compare two strings as in case sensitive or by ignoring case sensitivity we can use comopareToIgnoreCase()
function like below. The syntax and usage are the same as the compareTo() function.
//CompareTo Examples In Java Programming Language import java.util.*; public class CompareTo_Example { public static void main(String[] args) { String s1="ABC"; String s2="abc"; String s3="Abc"; System.out.println(s1+" Compare To "+s1+" and Result is "+s1.compareToIgnoreCase(s1)); System.out.println(s1+" Compare To "+s2+" and Result is "+s2.compareToIgnoreCase(s1)); System.out.println(s1+" Compare To "+s3+" and Result is "+s3.compareToIgnoreCase(s1)); } }


As we can see all string compare operations provided 0
which means equal with the case insensitive comparison.