Can someone explain the .compareTo string method to me in java?
Start by understanding why compare, .compareTo, and .equals exist in java: In Java the == evaluation on strings looks at the address. So if you have a string object s1 and an alias to it, s2, then the s1 == s2 will evaluate as true because they are pointing at the same address. On the other hand, if s1 and s2 are the same string, but different objects, then s1 == s2 will be false. This means other methods to evaluate the sameness or equality of strings are needed. Now that you know why they exist, why are there so many? The basic comparison of being the same string is .equals. s1.equals(s2) would look to see if they were exactly the same string. However, one reason to find out if strings are equal or not is because you want to put them in lexicographical order, AKA: Alphabetically. Here is where .compareTo comes in. If .compareTo. It walks through a string and looks for the first place they are different. Then it gives an answer based on that first different character that is the lexicographical distance. ``` public class Test { public static void main(String[] args) { String s1="This is a string."; String s2="This is a second string."; String s3="This is a second string."; String s4="s3 lies! It is the 3rd!"; String s5="This is a string?"; System.out.println(s1.compareTo(s2)); System.out.println(s3.compareTo(s2)); System.out.println(s1.compareTo(s4)); System.out.println(s1.compareToIgnoreCase(s4)); System.out.println(s1.compareTo(s5)); } } ``` 15 0 -31 1 -17 "This is a string." and "This is a second string." are the same up to the letter t ins string vs. e in second. \(\color{red}{e}\color{blue}{fghijiklmnopqrs}\color{red}{t}\) Note there are 15 characters between them and t is after e. Because t is after e the 15 steps are positive. The second number, 0, is because they are the same. Now, the -31 and 1 show something else about .compareTo. It uses the ASCII location. The difference is right at the start, T vs s. The ASCII sequence is: ABCDEFGHIJIKLMNOPQRS\(\color{red}{T}\)UVWXYZ[\]^_`abcdefghijiklmnopqr\(\color{red}{s}\)tuvwxyz The T is 31 characters before s. Thus, -31. But if I ignore case, T is 1 character after S, so 1. And the . to ? comparison in the last example shows that this also applies to other ASCII characters, not just letters.
Join our real-time social learning platform and learn together with your friends!