Comparable Interface

Objects that have an ordering are compared using the compareTo() method.

8. Comparing Strings


No. Upper and lower case matter.

Comparing Strings

Upper case characters are regarded as less than lower case characters. So "APPLE".compareTo("apple") returns a negative integer.

When strings are compared, case-sensitive dictionary order is used. Mostly this means that when two strings of the same case are compared, the one that appears first in the dictionary is less than the other. The technical term for this is lexicographic order. Here are the details:

Rule 1: If A.compareTo(B) == 0, then A and B are the same length (counting all characters, including blanks and punctuation) and each character in A is identical (including case) to the character in B at the same location.

"batcave".compareTo("batcave") == 0
"batcave".compareTo("bat cave") != 0

There are more rules on the following pages.

Question 8:

Decide which strings, when used with compareTo() return zero.

Zero or Not Zero
("mush room")