compareTo() method is used to compare the two specified Strings based on the alphabetical order(lexicographical order) of their characters.It returns positive number ,negative number or 0

Syntax:

Parameters:

anotherString: the String to be compared with the current String

Returns:

An integer value based on the case.

compare two Strings i.e. s1 and s2

if s1 > s2, it returns positive number

if s1 < s2, it returns negative number

if s1 == s2, it returns 0

Java String compareTo() Example 1:

Output:

Java String compareTo() Example 2:

OUTPUT:

Java String compareTo() Example 3:

Output:

Pin It on Pinterest

Share This