how to compareTo in java

public class Example {
    public static void main(String[] args) {
        String str1 = "Hello";
        String str2 = "World";

        int result = str1.compareTo(str2);

        if (result < 0) {
            System.out.println("str1 comes before str2");
        } else if (result > 0) {
            System.out.println("str2 comes before str1");
        } else {
            System.out.println("str1 and str2 are equal");
        }
    }
}