1
visibility

What will be the output of below statements?

String s1 = “abc”;
String s2 = “def”;
System.out.println(s1.compareTo(s2));

  • A

    0

  • B

    true

  • C

    -3

  • D

    false

compareTo method 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.