KnowledgeBoat Logo

Computer Applications

Which one of the given statements is true for the following statement? string1.compareTo(string2)

  1. if string1 > string2 the result will be a negative integer i.e. < 0.
  2. if string1 > string2 the result will be a positive integer i.e. > 0.
  3. if string1 > string2 the result will be 0.
  4. None of the above

Java String Handling

2 Likes

Answer

if string1 > string2 the result will be a positive integer i.e. > 0.

Reason — 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

Answered By

1 Like


Related Questions