Advertisement
Tóm Tắt
compareTo() method
The compareTo() method compares the values of two String objects and returns an int value after the comparison.
This comparison is performed by comparing the characters of both the String objects, on the basis of each index. To know more about the compareTo() method, let us take a look at its signature.
Signature of compareTo() method
public int compareTo(String str)
This method compares the value in the invoked String with the value in the String str and returns an int value, which could be:
- A zero, if the invoked String and String str have the same value.
- A positive value, if the invoked String has a greater value than the value in String str
- A negative value, if the invoked String has a smaller value than the value in String str
How is the returned int value of compareTo() method calculated?
This int value returned by the compareTo() method is calculated after finding
the difference between the ASCII values of characters of both String objects, at each index:
- When the two String objects being compared contain a same value,
the returned int value is zero because there is no difference between the ASCII values of characters at any index of both
the String objects. -
When the two String objects contain different values, the returned int value could be either positive or negative
after subtracting the ASCII value of the first non-matching
character present in the invoked String, from the ASCII value of the first non-matching
character present in the passing String value.
compareTo() example
Here in the program below, we have initialized three String objects and we are calling on compareTo() method to compare their values.
// Java - compareTo() method of String.
public class StringCompareTo
{
public static void main(String[] ar)
{
String firstString = new String("JavaPowerA");
String secondString= new String("JavaPowerA");
String thirdString = new String("JavaPowerF");
System.out.println(" First String is " + firstString);
System.out.println(" Second String is " + secondString);
System.out.println(" Third String is " + thirdString);
//comparison between ASCII values of first and second String
int i1 = firstString.compareTo(secondString);
System.out.println("Comparison b/w first and second String " + i1);
//comparison between ASCII values of first and third String
i1= firstString.compareTo(thirdString);
System.out.println("Comparison b/w first and third String " + i1);
}
}
Output is :
First String is JavaPowerA
Second String is JavaPowerA
Third String is JavaPowerF
Result of comparison b/w first and second String 0
Result of comparison b/w first and third String -5
Program Analysis
The first and second String objects have a same value, JavaPowerA, while the third String object has – JavaPowerF
- Comparison between the first and second String objects gives zero because they have the same value.
- Comparison between the first and third String objects gives -5, which is
the difference between the ASCII value of non-matching characters, A(65)and F(70).
Themethod compares the values of two String objects and returns anvalue after the comparison. This comparison is performed by comparing the characters of both the String objects, on the basis of each index. To know more about themethod, let us take a look at its signature.This int value returned by themethod is calculated after finding the difference between theof characters of both String objects, at each index:Here in the program below, we have initialized three String objects and we are calling on compareTo() method to compare their values.The first and second String objects have a same value,, while the third String object has –
Advertisement
Another example of compareTo()
// Java - compareTo() method of String.
class StringCompareTo
{
public static void main(String[] ar)
{
String firstString = new String("Java Thread");
String secondString = new String("Java Compare");
String thirdString = new String("Java Comparesss");
String fourthString = new String("Java Comparison");
System.out.println(" First String is " + firstString);
System.out.println(" Second String is " + secondString);
System.out.println(" Third String is " + thirdString);
System.out.println(" Fourth String is " + fourthString);
//comparison between ASCII values of first and second String
int i1 = firstString.compareTo(secondString);
System.out.println("Comparison b/w first and second String " + i1);
//comparison between ASCII values of second and third String
i1= secondString.compareTo(thirdString);
System.out.println("Comparison b/w second and third String " + i1);
//comparison between ASCII values of second and fourth String
i1= secondString.compareTo(fourthString);
System.out.println("Comparison b/w second and fourth String " + i1);
}
}
Output is :
First String is Java Thread
Second String is Java Compare
Third String is Java Comparesss
Fourth String is Java Comparison
Comparison b/w first and second String 17
Comparison b/w third and fourth String -3
Comparison b/w second and third String -4
Program Analysis
- Comparison between the first and second String objects gives 17, i.e. the difference between the ASCII value of non-matchcing characters, T(84)and C(67).
- Comparison between the second and third String objects gives -3 because the invoked second String(Java Compare) has 3 fewer character values than the third String(Java Comparesss), i.e. sss are missing in the second String,
after Java Compare is found similar in both the strings. - Comparison between the second and fourth String objects gives -4, i.e.
the difference between the ASCII value of non-matching characters, e(101)and i(105).
Please share this article –
Advertisement