How does compareTo method works internally in Java?

preview_player
Показать описание
#CompareToMethod #Java #String

Compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string. The result is a negative integer if this String object lexicographically precedes the argument string. The result is a positive integer if this String object lexicographically follows the argument string. The result is zero if the strings are equal; compareTo returns 0 exactly when the equals(Object) method would return true.

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 less than 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:


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:

The Java String compareTo() method is used for comparing two strings lexicographically. Each character of both the strings is converted into a Unicode value for comparison. The result is positive if the first string is lexicographically greater than the second string else the result would be negative.

what does compareTo return in Java?

The compareTo method is used when we need to determine the order of Strings lexicographically. It compares char values similar to the equals method. The compareTo method returns a negative integer if the first String object precedes the second string. It returns zero if the 2 strings being compared are equal.

Subsequently, question is, how do you use integer compareTo in Java? compareTo() method compares two Integer objects numerically. This method returns the value 0 if this Integer is equal to the argument Integer, a value less than 0 if this Integer is numerically less than the argument Integer and a value greater than 0 if this Integer is numerically greater than the argument Integer.

In this regard, what is compareTo ()?

The compareTo() method compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The method returns 0 if the string is equal to the other string.

Can we override compareTo method?

Override compareTo Method It should return a negative integer(usually -1), if the current triggering object is less than the passed one, and positive integer (usually +1) if greater than, and 0 if equal. compareTo method should throws an exception if the passed object has incompatible type or null.
Рекомендации по теме
Комментарии
Автор

Thanks for explaining in such an articulate manner.

rakibrafi
Автор

It helped me a lot.
Watching this just before exam.

tushar
Автор

Thank you so much, you saved my boards

aaryangotad
Автор

Love the explanation. In last return statement of ours compareTo method maybe we can put -> return 0; instead of ->return len1 - len2;. Looks like the result is the same (zero).

flankersukhoi
Автор

How does it works when strings holds numeric value? i.e String str = "9" & String str2 = "11"

moorthidhuruvasan
Автор

Sir are we using the ASCII values for calculating this?

sgiridharan
Автор

dude you typed abcedfghijklmnopqrstuvwxyz so fast lmao

Em