This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes java.lang.String.compareTo() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java.lang.String.compareTo() method" Watch "java.lang.String.compareTo() method" New topic

java.lang.String.compareTo() method

benny rusli
Ranch Hand

Joined: Jan 15, 2005
Posts: 72
Hello everyone,

can someone tell me, what is the meaning "Compares two strings lexicographically" ?

compareTo(String anotherString)-> Compares two strings lexicographically.

I tried to compare the following string :

and the return value result is -10, what is the meaning on this ?
The String "hello" is bigger than the string "how" or what ?
Any help will be appreciated.
Mike Noel
Ranch Hand

Joined: Dec 15, 2005
Posts: 108
The term lexicographically can be thought of as "alphabetically". That is, "apple" comes before "bug" and so on. Also, shorter words come before longer words. "apple" comes before "apples" This is also dictionary order. The only difference is that on a computer we also deal with special characters like !@#$%^&* that normally don't occur in dictionaries.

There are three results from comparing two strings s1 and s2.

s1 comes before s2
s1 equals s2
s1 comes after s2

That is reflected in the return value of the compareTo() method. If the result is < 0 it is case #1. If the result == 0 it is case #2. If the result is > 0 it is case #3. The magnitude of the result doesn't matter.

Hope that helps.


Mike Noel
benny rusli
Ranch Hand

Joined: Jan 15, 2005
Posts: 72
Hello everyone,

thanks Mike Noel, that was a great explanation, but what is the meaning from -10 the return value ? why not -1 or -2 ? thanks for any help.
Scott Johnson
Ranch Hand

Joined: Aug 24, 2005
Posts: 518
In the strings being compared, the first letter in both is 'h'. The second letter is different -- 'e' vs 'o'.

The method returns -10 because the letters 'e' and 'o' are 10 characters apart in UTF-16.

See the javadoc for more info.
[ January 13, 2006: Message edited by: Scott Johnson ]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
More specifically, "lexographically" means the ordering given by the Unicode values. Unicode assigns a integer value for each character. For letters, the Unicode values are in "alphabetical order" as described above. Unicode also specifies the ordering for punctuation marks and lower-case vs. upper-case letters.

I won't go into any more detail. If you are only using alphabetic characters, then you should think of it as comparing their order alphabetically.

Also, you shouldn't worry about the exact value that is returned. Look at the javadocs and you will see that the important part is the sign of the return value. You should check whether this return value is positive, negative, or zero to determine whether one String is "less than", "greater than", or the "equal to" another String. So in your example, the -10 indicates that "hello" is "less than" (or comes before) "how".


[ January 12, 2006: Message edited by: Layne Lund ]
[ January 12, 2006: Message edited by: Layne Lund ]

Java API Documentation
The Java Tutorial
I agree. Here's the link:
subject: java.lang.String.compareTo() method
It's not a secret anymore!