< 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 a same value.
  • Positive value, if the invoked String has a greater value than value in String str
  • Negative value, if the invoked String has a smaller value than 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 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);


//Comparision between ASCII values of first and second String
int i1 = firstString.compareTo(secondString);
System.out.println("Comparison b/w first and second String " + i1); 


//Comparision 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




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);


//Comparision between ASCII values of first and second String
int i1 = firstString.compareTo(secondString);
System.out.println("Comparison b/w first and second String " + i1); 


//Comparision between ASCII values of second and third String
i1= secondString.compareTo(thirdString);
System.out.println("Comparison b/w second and third String " + i1);


//Comparision 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






Please share this article -




< Prev
Next >
< == in String
ignoreCase() method >
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
Python

Ad2