Advertisement



< Prev
Next >



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:




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


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






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

C#, PYTHON and DJANGO


Advertisement