(^160) | Selection and Encapsulation
Comparison of strings in Java follows the collating sequence used in the Unicode char-
acter set. When the computer tests a relationship between two strings, it begins with the first
character of each one, compares the characters according to the collating sequence, and if they
are the same, repeats the comparison with the next character in each string. The character-by-
character test continues until either a mismatch is found or the final characters have been
compared and are equal. If all their characters are equal, then the two strings are equal. If a
mismatch is found, then the string with the character that comes before the other is the
“lesser” string.
For example, given the statements
String word1;
String word2;
word1 = "Tremendous";
word2 = "Small";
the following relational expressions have the values shown:
Expression Value Reason
word1.equals(word2) false They are unequal in the first character.
word1.compareTo(word2) > 0 true ’T’ comes after ‘S’ in the collating sequence.
word1.compareTo("Tremble") < 0 false The fifth characters don’t match, and ‘e’
comes after ‘b’.
word2.equals("Small") true They are equal.
"cat".compareTo("dog") == 0 false They are unequal.
Remembering the way that compareToworks can be a bit challenging at first. A conven-
tion that helps overcome this confusion is to write the relational expression with zero on the
right side of the operator. Then the operator has the same meaning as if we were able to sub-
stitute it for the method name. That is, writing
word1.compareTo(word2) > 0
has the same effect as if Java allowed us to write
word1 > word2
In most cases, the ordering of strings corresponds to alphabetical ordering. When strings
have mixed-case letters, however, we can get nonalphabetical results. For example, in a
phone book we would expect to see “Macauley” before “MacPherson.” The Unicode collating
sequence places all English uppercase letters before the lowercase letters, so the string
“MacPherson” compares less than “Macauley” in Java. To compare strings for strict alphabetical
ordering, all the characters must be in the same case. In the following examples,toLowerCase
and toUpperCaseare used to convert strings to a single case:
T
E
A
M
F
L
Y
Team-Fly®
やまだぃちぅ
(やまだぃちぅ)
#1