Advertisement



< Prev
Next >



compareTo() method





The compareTo() method returns an int value after comparing values of the two String objects.




Signature of compareTo() method


public int compareTo(String str)
Compares the value in the invoked String with the value in String str and returns an int value -
  • 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





Note :


This int value is calculated after finding the difference between the ASCII values of a character present at each index in both the String objects being compared.




compareTo() example


Here in the program below, we have initialized three String objects and we are calling on compareTo() method to compare their values.
// 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


First and second String objects have a value JavaPowerA, 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, i.e. the difference between the ASCII value of A(65)and F(70).



Advertisement




Another example of compareTo()


// 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 Compares
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 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 e(101)and i(105).




Please share this article -




< Prev
Next >
< == in String
ignoreCase() method >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement

Notifications



Please check our latest addition

PYTHON


Advertisement