The compareTo() function of the Java String class lexicographically compares the inputted string with the currently displayed string. It returns either a positive, negative, or 0.
The Unicode value of each character in the strings is used to compare the strings when comparing strings. Inferring the relative order of strings based on their character sequences is key.
Explanation
The compareTo() function of the Java String class lexicographically compares the inputted string with the currently displayed string. It returns either a positive, negative, or 0.
The Unicode value of each character in the strings is used to compare the strings when comparing strings.
A positive integer (difference of character value) is returned when the first string is lexicographically superior to the second string.
Syntax:
public int compareTo(String anotherString)
Parameter:
- string2 –A String that represents the second string that will be compared
- object- thing that represents the thing being compared
Return Value:
- An int value: 0 if the strings are equal, 0 if the strings are lexicographically shorter than the other strings, > 0 if the strings are longer (have more characters) than the other strings.
- If string1 is greater than string2, a positive number is returned.
- If string1 is less than string2, a negative integer is returned.
- It returns 0 if string1 and string2 are equal.
Example 1:
public class TechVidvanCompareTo {
public static void main(String argvs[]) {
String a1 = new String("INDIA IS MY COUNTRY");
String a2 = new String("india is my country");
System.out.println(a1.compareTo(a2));
}
}
Output:
-32
Example 2:
public class TechVidvanCompareTo{
public static void main(String args[]){
String b1="Hello";
String b2="";
String b3="Everyone";
System.out.println(b1.compareTo(b2));
System.out.println(b2.compareTo(b3));
}}
OUTPUT:
5
-7
Use of Java String CompareTo():
public class TechVidvanCompareTo{
public static void main(String args[]) {
String a1 = "hello";
String a2 = "hello";
String a3 = "meklo";
String a4 = "hemlo";
System.out.println(a1.compareTo(a2));
System.out.println(a1.compareTo(a3));
System.out.println(a1.compareTo(a4));
}
}
Output:
0
-5
-1
Java allows us to compare Strings based on their content and references. It is used in reference matching (by the == operator), sorting (by the compareTo() method), and authentication (by the equals() method).
Diagram:
Types of Java compareTo():
The compareTo() method has three different iterations, which are as follows:
- the use of int comparisonUsing the formula To(Object obj),
- compareTo(String anotherString),
- compareToIgnoreCase(String str)
Technical details:
Case Sensitivity:
Uppercase and lowercase characters are regarded differently by compareTo() since it is case-sensitive.
Null handling:
NullPointerException will be thrown if either the invoking string or anotherString is null.
Exception from String CompareTo():
- If this object cannot be compared to the supplied object, a ClassCastException will occur.
- Whenever the provided object is null, a NullPointerException is thrown.
Advantages of Java String CompareTo():
- Lexicographic Comparison: You can compare strings lexicographically (in a dictionary-style) with the compareTo() function.
- Standardized Comparison: It uses a standardized comparison algorithm, so while comparing strings, you can count on consistent results.
- Simplicity: Comparing strings is made easy and plain without the need to create original comparison logic.
Disadvantage of Java String CompareTo():
- Case Sensitivity: Because compareTo() is case-sensitive, it treats capital and lowercase letters differently. If you need to compare objects without regard to case, this could produce surprising results.
- The CompareTo method performs case- and culture-sensitive comparisons using both of its overloads. This approach cannot be used for ordinal comparisons or culturally blind comparisons.
Conclusion
We have extensively discussed the CompareTo() function in Java. We briefly discussed the use of the CompareTo() method and then moved on to see how using this method can help reduce a significant amount of code in a program.
