I know that compareTo
returns a negative or positive result on how well one string correlates to the other, but then why:
public class Test {
public static void main(String[] args) {
String y = "ab2";
if(y.compareTo("ac3") == -1) {
System.out.println("Test");
}
}
}
is true and
public class Test {
public static void main(String[] args) {
String y = "ab2";
if(y.compareTo("ab3") == -1) {
System.out.println("Test");
}
}
}
is also true?
==-1
but<0
. Notice that"ab2".compareTo("cb2")
returns -2, just like"ab2".compareTo("ab4")
(result is difference in Unicode Table of character which are different in string indexes - if length of strings is same). – PhilippcompareTo
does not use correlation: correlation is a symmetric property, whereascompareTo
is (or at least should be) anti-symmetric, at least in the sense thatsign(a.compareTo(b)) = -sign(b.compareTo(a))
. – Asterism