alxmke
alxmke

Reputation: 334

Comparing Java Strings with the `compareTo()` method

As a generalisation of string comparisons made by the compareTo() method:

For two given letters:

This may be considered as such:

In the order of 26 English-language letters, "A" may be considered as index 1, and "B" to be index 2:

Therefore, A = 1; B = 2.

A - B = -1.

To reach the value A, B must be reduced by 1, or B - 1 = 1 = A.

For example:

  int result;
  
  String letterA = "a";
  String letterB = "b";
  
  result = letterA.compareTo(letterB);
  System.out.println("The difference between the letter \"a\" and the letter \"b\" will be equivalent to: " + result);

When considering a string of characters:

The difference of the alphabetical index of the first two characters whose string-character indices along their respective strings are equivalent and whose alphabetical indices do not match will be the value returned by the String.compareTo(String) method (where "String" represents a generalised string value).

For example:

  int result;
  
  String stringBA = "ba";
  String stringBD = "bd";
  
  result = stringBA.compareTo(stringBD);
  System.out.println("The difference between string \"ba\" and string \"bd\" will be equivalent to: " + result);

The comparison will evaluate to -3, as the difference between the letters ultimately compared within the method are the characters "a" and "d", where their alphabetical indices are 1 and 4, respectively.

This may be said as: To reach the value of A, D must be reduced by 3, or D - 3 = A.

Could this be considered accurate?

Upvotes: 2

Views: 2170

Answers (2)

Stephen C
Stephen C

Reputation: 719709

Could this be considered accurate?

It depends on what you mean by accurate.

  • As a specification, it is incomplete:

    • It only deals with characters that are "letters" and strings containing "letters". By contrast, String.compareTo works for any sequence(s) of codepoints.

    • It does not deal with Strings of unequal length which have the same character subsequences for their common part.

  • Ignoring the holes in the spec, it is an over-specification. The javadoc for String.compareTo does not specify precisely what the returned value is. It just says whether it is zero, negative or positive.

  • Ignoring the holes, it looks like it is an adequate model for the behaviour of (at least) the implementation of compareTo that @user3580294 has found.

The real specification for String.compareTo is given by the javadocs. It is unambiguous, and definitive. There is no need to restate it in other words ... IMO.

Upvotes: 1

awksp
awksp

Reputation: 11877

Here's the source for String#compareTo():

public int compareTo(String anotherString) {
    int len1 = value.length;
    int len2 = anotherString.value.length;
    int lim = Math.min(len1, len2);
    char v1[] = value;
    char v2[] = anotherString.value;

    int k = 0;
    while (k < lim) {
        char c1 = v1[k];
        char c2 = v2[k];
        if (c1 != c2) {
            return c1 - c2;
        }
        k++;
    }
    return len1 - len2;
}

Based on this, I believe you are correct. Either the difference between the first pair of nonmatching characters at a given index is returned or the difference in lengths is returned.

Upvotes: 1

Related Questions