What is return value by function compareTo () If the invoking string is greater than the string compared?



Description

The java.lang.String.compareTo() method compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string.

  • The result is a negative integer if this String object lexicographically precedes the argument string.
  • The result is a positive integer if this String object lexicographically follows the argument string.
  • The result is zero if the strings are equal, compareTo returns 0 exactly when the equals(Object) method would return true.

Declaration

Following is the declaration for java.lang.String.compareTo() method

public int compareTo(String anotherString)

Parameters

anotherString − This is the String to be compared.

Return Value

This method returns the value 0 if the argument string is equal to this string, a value less than 0 if this string is lexicographically less than the string argument and a value greater than 0 if this string is lexicographically greater than the string argument.

Exception

NA

Example

The following example shows the usage of java.lang.String.compareTo() method.

package com.tutorialspoint;

import java.lang.*;

public class StringDemo {

   public static void main(String[] args) {

      String str1 = "tutorials", str2 = "point";
   
      // comparing str1 and str2
      int retval = str1.compareTo(str2);

      // prints the return value of the comparison
      if (retval > 0) {
         System.out.println("str1 is greater than str2");
      } else if (retval == 0) {
         System.out.println("str1 is equal to str2");
      } else {
         System.out.println("str1 is less than str2");
      }
   }
}

Let us compile and run the above program, this will produce the following result −

str1 is greater than str2

java_lang_string.htm

The Java String class compareTo() method compares the given string with the current string lexicographically. It returns a positive number, negative number, or 0.

It compares strings on the basis of the Unicode value of each character in the strings.

If the first string is lexicographically greater than the second string, it returns a positive number (difference of character value). If the first string is less than the second string lexicographically, it returns a negative number, and if the first string is lexicographically equal to the second string, it returns 0.

Syntax

The method accepts a parameter of type String that is to be compared with the current string.

It returns an integer value. It throws the following two exceptions:

ClassCastException: If this object cannot get compared with the specified object.

NullPointerException: If the specified object is null.

Internal implementation

Java String compareTo() Method Example

FileName: CompareToExample.java

Test it Now

Output:

Java String compareTo(): empty string

When we compare two strings in which either first or second string is empty, the method returns the length of the string. So, there may be two scenarios:

  • If first string is an empty string, the method returns a negative
  • If second string is an empty string, the method returns a positive number that is the length of the first string.

FileName: CompareToExample2.java

Test it Now

Output:

Java String compareTo(): case sensitive

To check whether the compareTo() method considers the case sensitiveness of characters or not, we will make the comparison between two strings that contain the same letters in the same sequence.

Suppose, a string having letters in uppercase, and the second string having the letters in lowercase. On comparing these two string, if the outcome is 0, then the compareTo() method does not consider the case sensitiveness of characters; otherwise, the method considers the case sensitiveness of characters.

FileName: CompareToExample3.java

Output:

Conclusion: It is obvious by looking at the output that the outcome is not equal to zero. Hence, the compareTo() method takes care of the case sensitiveness of characters.

Java String compareTo(): ClassCastException

The ClassCastException is thrown when objects of incompatible types get compared. In the following example, we are comparing an object of the ArrayList (al) with a string literal ("Sehwag").

FileName: CompareToExample4.java

Output:

Exception in thread "main" java.lang.ClassCastException: class Players cannot be cast to class java.lang.Comparable

Java String compareTo(): NullPointerException

The NullPointerException is thrown when a null object invokes the compareTo() method. Observe the following example.

FileName: CompareToExample5.java

Output:

Exception in thread "main" java.lang.NullPointerException
	at CompareToExample5.main(CompareToExample5.java:9)


What is the value returned by function compareTo () If the invoking string is greater?

Explanation: compareTo() function returns zero when both the strings are equal, it returns a value less than zero if the invoking string is less than the other string being compared and value greater than zero when invoking string is greater than the string compared to.

What is the value returned by function compareTo () If the invoking string is greater than the string compare?

Java String compareTo() Method The method returns 0 if the string is equal to the other string. A value less than 0 is returned if the string is less than the other string (less characters) and a value greater than 0 if the string is greater than the other string (more characters).

What is the return type of string compareTo () method?

The Java String class compareTo() method compares the given string with the current string lexicographically. It returns a positive number, negative number, or 0. It compares strings on the basis of the Unicode value of each character in the strings.

What does a negative return value indicate when calling the compareTo method on a string?

compareTo() Return Values compareTo() in java returns an integer value. It returns a positive integer if string1 is lexicographically greater than string2, negative if string2 is greater than string1, and zero if both are equal.