KnowledgeBoat Logo

Computer Applications

Mention the purpose and syntax of compareTo() method.

Java String Handling

1 Like

Answer

The compareTo() method compares two strings lexicographically, and returns a value based on the following logic:

  1. if string1 > string2 the result will be a positive integer, i.e., result > 0
  2. if string1 < string2 the result will be a negative integer, i.e., result < 0
  3. if string1 = string2 the result will be 0, i.e., result = 0

Syntax:
string1.compareTo(string2)

Answered By

1 Like


Related Questions