NikS
NikS

Reputation: 149

Java: comparing strings with "-"(dash)

I am beginner in Java and probably I am asking something obvious but anyway.

According to Java.Lang.String compareTo() method description:

The comparison is based on the Unicode value of each character in the strings.

I am comparing strings

    public void compareString() {
    String str1 = "test-2014";
    String str2 = "test195519-9022c72bc161";
    String str3 = "test200101-ee4d99b1492c";
    String str4 = "test212941-884e3f03fe1e";

    System.out.println(str1.compareTo(str2));
    System.out.println(str1.compareTo(str3));
    System.out.println(str1.compareTo(str4));
}
------------ OUTPUT -----------
-4
-5
-5

I am expecting the output will be the same in all three cases because Unicode value of fifth char in every case (1, 2, 2) is greater than fifth char in "str1" (-).

Could you please explain why it happens?

Thinks

Upvotes: 2

Views: 3488

Answers (3)

TypeIA
TypeIA

Reputation: 17258

Nowhere does it say that compareTo() should return the index of the first character that doesn't match. This is an incorrect assumption on your part. The only guaranteed result is that its return value will be either negative, positive or zero, depending on the lexicographical comparison of the two strings.

Within each of those 3 possibilities, the actual number that gets returned (-5 vs. -1 vs. -3495582) depends on the implementation (the implementation is free to return whatever it wants, as long as it's negative) and you should make no further assumptions about it.

Upvotes: 0

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280102

You expect the following

I am expecting the output will be the same in all three cases because Unicode value of fifth char in every case (1, 2, 2) is greater than fifth char in "str1" (-).

This expectation is wrong. The unicode value of - is smaller than the others.

Run this test

System.out.println((int) '-');
System.out.println((int) '1');
System.out.println((int) '2');

It will print

45
49
50

45 is smaller than 49, so the method will return a negative value. As the javadoc says

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.

For reference, the implementation of String#compareTo(String) in Java Oracle 7

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; // important!!!!
        }
        k++;
    }
    return len1 - len2;
}

Upvotes: 1

rgettman
rgettman

Reputation: 178303

It doesn't matter which negative number is returned. The only important piece of information here is the sign of what compareTo returns. It wouldn't matter if it returned -1, -4, -5, or -1000000. The only thing to conclude is that str1 is less than each of the other strings.

From the Javadocs for the compareTo method in Comparable:

Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

Upvotes: 4

Related Questions