Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

compareTo() definition mistake?

https://www.tutorialspoint.com/java/lang/string_compareto.htm

Example code of compareTo does not allign with the definition. Or am I wrong?

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 


1 Answer
Pythonista

The example works correctly. The compareTo() method is called by a string object and takes another string object as argument. The return value is integer and is difference in Unicode values of characters of respective strings when they are not equal. The value can be -ve, 0 or +ve.

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.