Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

compareTo  RSS feed

 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the base in order to compare strings?

The compareTo method is used when we need to determine the order of Strings lexicographically

http://www.javabeginner.com/learn-java/java-string-comparison

What does it mean?

For example: "this is my cat"

"green road"

What is the base in order to compare these sentences?








 
Frederick Winsnes
Greenhorn
Posts: 17
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the base in order to compare strings?

What do you mean with base?
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How two strings compare?
How a string be greater?
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you read the documentation? Because there it is mentioned in the second sentence.
 
Frederick Winsnes
Greenhorn
Posts: 17
Android Chrome Eclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abalfazl hossein wrote:How two strings compare?
How a string be greater?


 
prem pillai
Ranch Hand
Posts: 87
Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If not satisfied by java doc , you can have a look at the below link to understand what is lexicographical order !!
http://en.wikipedia.org/wiki/Lexicographical_order
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The output of the statements is
1
-31
32
0
The fi rst int displayed from line 8 is positive because the string “ hello ” is greater than
“ goodbye “ . The actual value of the positive number is normally irrelevant, and for String
objects it represents the difference between the fi rst unequal characters between the two
strings. Line 9 compares “ Hello ” to “ goodbye ” and outputs - 31 because H is uppercase and
appears before all lowercase letters. Therefore, “ Hello ” is less than “ goodbye “ . Similarly,
“ hello ” is greater than “ Hello ” on line 10, which outputs 32 . Line 11 outputs 0 because
the two strings are equal


From:

Sun Certified Programmer
for Java® Platform, SE6
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!