• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

java.lang.String.compareTo() method

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

_M_
 
benny rusli
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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".

Layne

[ January 12, 2006: Message edited by: Layne Lund ]
[ January 12, 2006: Message edited by: Layne Lund ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic