[Solved] Comparing strings is “1”


A comparison of Strings in Java is done character by character. Each character has a specific ranking to it based on where it appears in the Unicode character table (for this case, we can use ASCII, since it’s English).

“1” would be considered less than “7”, as well as “T”.

To invoke (place this inside of main():

System.out.println("1".compareTo("7"));
System.out.println("1".compareTo("Test"));
System.out.println("1".compareToIgnoreCase("7"));
System.out.println("1".compareToIgnoreCase("Test"));

You’ll get negative valued results – these are the distances in terms of ASCII point from the character you’re comparing to. compareToIgnoreCase() compares the values within the lowercase ASCII range, so this is why the value of the last compareToIgnoreCase() is so low.

1

solved Comparing strings is “1”<"seven" [closed]