cupojava
cupojava

Reputation: 79

What does the String classes compareTo() method return

In the Java API on oracles website: "compareTo Returns: "the value 0 if the argument string is equal to this string; a value less than 0 if this string is lexicographically less than the string argument; and a value greater than 0 if this string is lexicographically greater than the string argument." "

Here is an if statement:

String a = "abd";
String b = "abc";

if(a.compareTo(b) >= 1)

returns true since string a is greater, lexicographically.

My question is, does the compareTo always return a 0, 1, or -1? or does it return the actual amount that the string is greater than or less than the string argument.

So in the above if statement, since "abd" is one greater than "abc" is it returning 1?

Upvotes: 1

Views: 21956

Answers (3)

CrippledTable
CrippledTable

Reputation: 816

Falmarri fully answered this question; as opposed to only indicating the conditions in which the return value would be positive, negative or zero.

"This is the definition of lexicographic ordering. If two strings are different, then either they have different characters at some index that is a valid index for both strings, or their lengths are different, or both. If they have different characters at one or more index positions, let k be the smallest such index; then the string whose character at position k has the smaller value, as determined by using the < operator, lexicographically precedes the other string. In this case, compareTo returns the difference of the two character values at position k in the two string -- that is, the value:

this.charAt(k)-anotherString.charAt(k)

If there is no index position at which they differ, then the shorter string lexicographically precedes the longer string. In this case, compareTo returns the difference of the lengths of the strings -- that is, the value:

this.length()-anotherString.length()"

Upvotes: 0

Diego C Nascimento
Diego C Nascimento

Reputation: 2801

According to http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#compareTo%28java.lang.String%29

In this case, compareTo returns the difference of the two character values at position k >in the two string -- that is, the value:

this.charAt(k)-anotherString.charAt(k)

If there is no index position at which they differ, then the shorter string lexicographically precedes the longer string. In this case, compareTo returns the difference of the lengths of the strings -- that is, the value:

this.length()-anotherString.length()

For the last case, for the lengths of the String, by documentation that seems it can return other than -1, 0, 1

Upvotes: 5

As far as you're concerned, there's no telling what the magnitude of the compareTo return value is, just the sign. In practice, most compareTo implementations will return -1, 0, or 1, but the contract specifically says positive or negative, and you should write your code accordingly (e.g., using int compare = a.compareTo(b); if(compare > 0) {...} else...).

Upvotes: 5

Related Questions